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:
DreamPiggy 2019-03-31 15:36:04 +08:00
parent 4235e3e302
commit f9e80d18eb
4 changed files with 21 additions and 15 deletions

View File

@ -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;

View File

@ -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

View File

@ -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;
/**

View File

@ -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];