Fix the current caches manager, does not apply the SDImageCache.sharedImageCache. Fix the thread safe issue of caches manager.
Update the test cases and documents about this behavior.
This commit is contained in:
parent
4235e3e302
commit
f9e80d18eb
|
@ -19,7 +19,7 @@ typedef NS_ENUM(NSUInteger, SDImageCachesManagerOperationPolicy) {
|
|||
@interface SDImageCachesManager : NSObject <SDImageCache>
|
||||
|
||||
/**
|
||||
Returns the global shared caches manager instance.
|
||||
Returns the global shared caches manager instance. By default we will set [`SDImageCache.sharedImageCache`] into the caches array.
|
||||
*/
|
||||
@property (nonatomic, class, readonly, nonnull) SDImageCachesManager *sharedManager;
|
||||
|
||||
|
|
|
@ -8,6 +8,13 @@
|
|||
|
||||
#import "SDImageCachesManager.h"
|
||||
#import "SDImageCachesManagerOperation.h"
|
||||
#import "SDImageCache.h"
|
||||
|
||||
@interface SDImageCachesManager ()
|
||||
|
||||
@property (nonatomic, strong, nonnull) dispatch_semaphore_t cachesLock;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SDImageCachesManager
|
||||
|
||||
|
@ -28,6 +35,9 @@
|
|||
self.removeOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
|
||||
self.containsOperationPolicy = SDImageCachesManagerOperationPolicySerial;
|
||||
self.clearOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
|
||||
// initialize with default image caches
|
||||
self.caches = @[[SDImageCache sharedImageCache]];
|
||||
self.cachesLock = dispatch_semaphore_create(1);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@ -38,21 +48,25 @@
|
|||
if (![cache conformsToProtocol:@protocol(SDImageCache)]) {
|
||||
return;
|
||||
}
|
||||
SD_LOCK(self.cachesLock);
|
||||
NSMutableArray<id<SDImageCache>> *mutableCaches = [self.caches mutableCopy];
|
||||
if (!mutableCaches) {
|
||||
mutableCaches = [NSMutableArray array];
|
||||
}
|
||||
[mutableCaches addObject:cache];
|
||||
self.caches = [mutableCaches copy];
|
||||
SD_UNLOCK(self.cachesLock);
|
||||
}
|
||||
|
||||
- (void)removeCache:(id<SDImageCache>)cache {
|
||||
if (![cache conformsToProtocol:@protocol(SDImageCache)]) {
|
||||
return;
|
||||
}
|
||||
SD_LOCK(self.cachesLock);
|
||||
NSMutableArray<id<SDImageCache>> *mutableCaches = [self.caches mutableCopy];
|
||||
[mutableCaches removeObject:cache];
|
||||
self.caches = [mutableCaches copy];
|
||||
SD_UNLOCK(self.cachesLock);
|
||||
}
|
||||
|
||||
#pragma mark - SDImageCache
|
||||
|
|
|
@ -10,6 +10,9 @@
|
|||
|
||||
@interface SDImageLoadersManager : NSObject <SDImageLoader>
|
||||
|
||||
/**
|
||||
Returns the global shared loaders manager instance. By default we will set [`SDWebImageDownloader.sharedDownloader`] into the loaders array.
|
||||
*/
|
||||
@property (nonatomic, class, readonly, nonnull) SDImageLoadersManager *sharedManager;
|
||||
|
||||
/**
|
||||
|
|
|
@ -27,14 +27,6 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
|||
|
||||
@implementation SDImageCacheTests
|
||||
|
||||
+ (void)setUp {
|
||||
[[SDImageCachesManager sharedManager] addCache:[SDImageCache sharedImageCache]];
|
||||
}
|
||||
|
||||
+ (void)tearDown {
|
||||
[[SDImageCachesManager sharedManager] removeCache:[SDImageCache sharedImageCache]];
|
||||
}
|
||||
|
||||
- (void)test01SharedImageCache {
|
||||
expect([SDImageCache sharedImageCache]).toNot.beNil();
|
||||
}
|
||||
|
@ -452,8 +444,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
|||
SDImageCachesManager *cachesManager = [[SDImageCachesManager alloc] init];
|
||||
SDImageCache *cache1 = [[SDImageCache alloc] initWithNamespace:@"cache1"];
|
||||
SDImageCache *cache2 = [[SDImageCache alloc] initWithNamespace:@"cache2"];
|
||||
[cachesManager addCache:cache1];
|
||||
[cachesManager addCache:cache2];
|
||||
cachesManager.caches = @[cache1, cache2];
|
||||
|
||||
[[NSFileManager defaultManager] removeItemAtPath:cache1.diskCachePath error:nil];
|
||||
[[NSFileManager defaultManager] removeItemAtPath:cache2.diskCachePath error:nil];
|
||||
|
@ -502,8 +493,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
|||
SDImageCachesManager *cachesManager = [[SDImageCachesManager alloc] init];
|
||||
SDImageCache *cache1 = [[SDImageCache alloc] initWithNamespace:@"cache1"];
|
||||
SDImageCache *cache2 = [[SDImageCache alloc] initWithNamespace:@"cache2"];
|
||||
[cachesManager addCache:cache1];
|
||||
[cachesManager addCache:cache2];
|
||||
cachesManager.caches = @[cache1, cache2];
|
||||
|
||||
[[NSFileManager defaultManager] removeItemAtPath:cache1.diskCachePath error:nil];
|
||||
[[NSFileManager defaultManager] removeItemAtPath:cache2.diskCachePath error:nil];
|
||||
|
@ -543,8 +533,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
|||
SDImageCachesManager *cachesManager = [[SDImageCachesManager alloc] init];
|
||||
SDImageCache *cache1 = [[SDImageCache alloc] initWithNamespace:@"cache1"];
|
||||
SDImageCache *cache2 = [[SDImageCache alloc] initWithNamespace:@"cache2"];
|
||||
[cachesManager addCache:cache1];
|
||||
[cachesManager addCache:cache2];
|
||||
cachesManager.caches = @[cache1, cache2];
|
||||
|
||||
[[NSFileManager defaultManager] removeItemAtPath:cache1.diskCachePath error:nil];
|
||||
[[NSFileManager defaultManager] removeItemAtPath:cache2.diskCachePath error:nil];
|
||||
|
|
Loading…
Reference in New Issue