Removing not needed functions in SDImageCache. Using DI with initializer.
This commit is contained in:
parent
5fbbf73986
commit
b51e0ca689
|
@ -79,7 +79,18 @@ typedef void(^SDWebImageCompletionWithPossibleErrorBlock)(NSError * _Nullable er
|
|||
* @param directory Directory to cache disk images in
|
||||
*/
|
||||
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
|
||||
diskCacheDirectory:(nonnull NSString *)directory NS_DESIGNATED_INITIALIZER;
|
||||
diskCacheDirectory:(nonnull NSString *)directory;
|
||||
|
||||
/**
|
||||
* Init a new cache store with a specific namespace and directory
|
||||
*
|
||||
* @param ns The namespace to use for this cache store
|
||||
* @param directory Directory to cache disk images in
|
||||
* @param fileManager The file manager for storing image, if nil then will be created new one
|
||||
*/
|
||||
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
|
||||
diskCacheDirectory:(nonnull NSString *)directory
|
||||
fileManager:(nullable NSFileManager *)fileManager NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
#pragma mark - Cache paths
|
||||
|
||||
|
@ -136,17 +147,6 @@ typedef void(^SDWebImageCompletionWithPossibleErrorBlock)(NSError * _Nullable er
|
|||
toDisk:(BOOL)toDisk
|
||||
completion:(nullable SDWebImageCompletionWithPossibleErrorBlock)completionBlock;
|
||||
|
||||
/**
|
||||
* Synchronously store image NSData into disk cache at the given key.
|
||||
*
|
||||
* @warning This method is synchronous, make sure to call it from the ioQueue
|
||||
*
|
||||
* @param imageData The image data to store
|
||||
* @param key The unique image cache key, usually it's image absolute URL
|
||||
*/
|
||||
- (void)storeImageDataToDisk:(nullable NSData *)imageData
|
||||
forKey:(nullable NSString *)key;
|
||||
|
||||
/**
|
||||
* Synchronously store image NSData into disk cache at the given key.
|
||||
*
|
||||
|
@ -160,20 +160,6 @@ typedef void(^SDWebImageCompletionWithPossibleErrorBlock)(NSError * _Nullable er
|
|||
forKey:(nullable NSString *)key
|
||||
error:(NSError * _Nullable * _Nullable)errorPtr;
|
||||
|
||||
/**
|
||||
* Synchronously store image NSData into disk cache at the given key.
|
||||
*
|
||||
* @warning This method is synchronous, make sure to call it from the ioQueue
|
||||
*
|
||||
* @param imageData The image data to store
|
||||
* @param key The unique image cache key, usually it's image absolute URL
|
||||
* @param fileManager The file manager for storing image. If nil, then will be used local object.
|
||||
* @param errorPtr NSError pointer. If error occurs then (*errorPtr) != nil.
|
||||
*/
|
||||
- (BOOL)storeImageDataToDisk:(nullable NSData *)imageData
|
||||
forKey:(nullable NSString *)key
|
||||
fileManager:(nullable NSFileManager *)fileManager
|
||||
error:(NSError * _Nullable * _Nullable)errorPtr;
|
||||
|
||||
#pragma mark - Query and Retrieve Ops
|
||||
|
||||
|
|
|
@ -84,6 +84,12 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
|
|||
|
||||
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
|
||||
diskCacheDirectory:(nonnull NSString *)directory {
|
||||
return [self initWithNamespace:ns diskCacheDirectory:directory fileManager: nil];
|
||||
}
|
||||
|
||||
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
|
||||
diskCacheDirectory:(nonnull NSString *)directory
|
||||
fileManager:(nullable NSFileManager *)fileManager {
|
||||
if ((self = [super init])) {
|
||||
NSString *fullNamespace = [@"com.hackemist.SDWebImageCache." stringByAppendingString:ns];
|
||||
|
||||
|
@ -95,7 +101,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
|
|||
// Init the memory cache
|
||||
_memCache = [[AutoPurgeCache alloc] init];
|
||||
_memCache.name = fullNamespace;
|
||||
|
||||
|
||||
// Init the disk cache
|
||||
if (directory != nil) {
|
||||
_diskCachePath = [directory stringByAppendingPathComponent:fullNamespace];
|
||||
|
@ -103,33 +109,40 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
|
|||
NSString *path = [self makeDiskCachePath:ns];
|
||||
_diskCachePath = path;
|
||||
}
|
||||
|
||||
dispatch_sync(_ioQueue, ^{
|
||||
_fileManager = [NSFileManager new];
|
||||
});
|
||||
|
||||
|
||||
if (fileManager == nil) {
|
||||
dispatch_sync(_ioQueue, ^{
|
||||
_fileManager = [NSFileManager new];
|
||||
});
|
||||
} else {
|
||||
_fileManager = fileManager;
|
||||
}
|
||||
|
||||
|
||||
#if SD_UIKIT
|
||||
// Subscribe to app events
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(clearMemory)
|
||||
name:UIApplicationDidReceiveMemoryWarningNotification
|
||||
object:nil];
|
||||
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(deleteOldFiles)
|
||||
name:UIApplicationWillTerminateNotification
|
||||
object:nil];
|
||||
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(backgroundDeleteOldFiles)
|
||||
name:UIApplicationDidEnterBackgroundNotification
|
||||
object:nil];
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
return self;
|
||||
|
||||
}
|
||||
|
||||
|
||||
- (void)dealloc {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
SDDispatchQueueRelease(_ioQueue);
|
||||
|
@ -240,29 +253,17 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
|
|||
}
|
||||
}
|
||||
|
||||
- (void)storeImageDataToDisk:(nullable NSData *)imageData
|
||||
forKey:(nullable NSString *)key {
|
||||
[self storeImageDataToDisk:imageData forKey:key fileManager:_fileManager error:nil];
|
||||
}
|
||||
|
||||
- (BOOL)storeImageDataToDisk:(nullable NSData *)imageData
|
||||
forKey:(nullable NSString *)key
|
||||
error:(NSError * _Nullable * _Nullable)errorPtr {
|
||||
return [self storeImageDataToDisk:imageData forKey:key fileManager:_fileManager error:errorPtr];
|
||||
}
|
||||
|
||||
- (BOOL)storeImageDataToDisk:(nullable NSData *)imageData
|
||||
forKey:(nullable NSString *)key
|
||||
fileManager:(nullable NSFileManager *)fileManager
|
||||
error:(NSError * _Nullable * _Nullable)errorPtr {
|
||||
if (!imageData || !key) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
[self checkIfQueueIsIOQueue];
|
||||
|
||||
if (![fileManager fileExistsAtPath:_diskCachePath]) {
|
||||
[fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
|
||||
if (![_fileManager fileExistsAtPath:_diskCachePath]) {
|
||||
[_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
|
||||
}
|
||||
|
||||
// get cache Path for image key
|
||||
|
@ -270,7 +271,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
|
|||
// transform to NSUrl
|
||||
NSURL *fileURL = [NSURL fileURLWithPath:cachePathForKey];
|
||||
|
||||
if (![fileManager createFileAtPath:cachePathForKey contents:imageData attributes:nil] && errorPtr) {
|
||||
if (![_fileManager createFileAtPath:cachePathForKey contents:imageData attributes:nil] && errorPtr) {
|
||||
*errorPtr = [[NSError alloc] initWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
|
||||
return NO;
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
|
||||
/* Begin PBXBuildFile section */
|
||||
1E3C51E919B46E370092B5E6 /* SDWebImageDownloaderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E3C51E819B46E370092B5E6 /* SDWebImageDownloaderTests.m */; };
|
||||
37D122881EC48B5E00D98CEB /* MockFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 37D122871EC48B5E00D98CEB /* MockFileManager.m */; };
|
||||
2D7AF0601F329763000083C2 /* SDTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D7AF05F1F329763000083C2 /* SDTestCase.m */; };
|
||||
37D122881EC48B5E00D98CEB /* MockFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 37D122871EC48B5E00D98CEB /* MockFileManager.m */; };
|
||||
433BBBB51D7EF5C00086B6E9 /* SDWebImageDecoderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 433BBBB41D7EF5C00086B6E9 /* SDWebImageDecoderTests.m */; };
|
||||
433BBBB71D7EF8200086B6E9 /* TestImage.gif in Resources */ = {isa = PBXBuildFile; fileRef = 433BBBB61D7EF8200086B6E9 /* TestImage.gif */; };
|
||||
433BBBB91D7EF8260086B6E9 /* TestImage.png in Resources */ = {isa = PBXBuildFile; fileRef = 433BBBB81D7EF8260086B6E9 /* TestImage.png */; };
|
||||
|
@ -31,10 +31,10 @@
|
|||
/* Begin PBXFileReference section */
|
||||
1DAAA77E3CA7387F702040D9 /* Pods_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
1E3C51E819B46E370092B5E6 /* SDWebImageDownloaderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDWebImageDownloaderTests.m; sourceTree = "<group>"; };
|
||||
37D122861EC48B5E00D98CEB /* MockFileManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MockFileManager.h; sourceTree = "<group>"; };
|
||||
37D122871EC48B5E00D98CEB /* MockFileManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MockFileManager.m; sourceTree = "<group>"; };
|
||||
2D7AF05E1F329763000083C2 /* SDTestCase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDTestCase.h; sourceTree = "<group>"; };
|
||||
2D7AF05F1F329763000083C2 /* SDTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDTestCase.m; sourceTree = "<group>"; };
|
||||
37D122861EC48B5E00D98CEB /* MockFileManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MockFileManager.h; sourceTree = "<group>"; };
|
||||
37D122871EC48B5E00D98CEB /* MockFileManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MockFileManager.m; sourceTree = "<group>"; };
|
||||
433BBBB41D7EF5C00086B6E9 /* SDWebImageDecoderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDWebImageDecoderTests.m; sourceTree = "<group>"; };
|
||||
433BBBB61D7EF8200086B6E9 /* TestImage.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = TestImage.gif; sourceTree = "<group>"; };
|
||||
433BBBB81D7EF8260086B6E9 /* TestImage.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = TestImage.png; sourceTree = "<group>"; };
|
||||
|
|
|
@ -193,7 +193,7 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
|
|||
- (void)test40InsertionOfImageData {
|
||||
|
||||
NSData *imageData = [NSData dataWithContentsOfFile:[self testImagePath]];
|
||||
[self.sharedImageCache storeImageDataToDisk:imageData forKey:kImageTestKey];
|
||||
[self.sharedImageCache storeImageDataToDisk:imageData forKey:kImageTestKey error:nil];
|
||||
|
||||
UIImage *storedImageFromMemory = [self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey];
|
||||
expect(storedImageFromMemory).to.equal(nil);
|
||||
|
@ -211,23 +211,29 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
|
|||
}];
|
||||
}
|
||||
|
||||
- (void)test41StoreImageDataToDistWithError {
|
||||
- (void)test41StoreImageDataToDiskWithError {
|
||||
NSData *imageData = [NSData dataWithContentsOfFile:[self testImagePath]];
|
||||
NSError * error = nil;
|
||||
[self.sharedImageCache storeImageDataToDisk:imageData
|
||||
forKey:kImageTestKey
|
||||
fileManager:[[MockFileManager alloc] initWithSendError:EACCES]
|
||||
error:&error];
|
||||
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:@"test"
|
||||
diskCacheDirectory:@"/"
|
||||
fileManager:[[MockFileManager alloc] initWithSendError:EACCES]];
|
||||
[cache storeImageDataToDisk:imageData
|
||||
forKey:kImageTestKey
|
||||
error:&error];
|
||||
|
||||
XCTAssertEqual(error.code, EACCES);
|
||||
}
|
||||
|
||||
- (void)test42StoreImageDataToDiskWithoutError {
|
||||
NSData *imageData = [NSData dataWithContentsOfFile:[self testImagePath]];
|
||||
NSError * error = nil;
|
||||
[self.sharedImageCache storeImageDataToDisk:imageData
|
||||
forKey:kImageTestKey
|
||||
fileManager:[[MockFileManager alloc] initWithSendError:0]
|
||||
error:&error];
|
||||
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:@"test"
|
||||
diskCacheDirectory:@"/"
|
||||
fileManager:[[MockFileManager alloc] initWithSendError:0]];
|
||||
[cache storeImageDataToDisk:imageData
|
||||
forKey:kImageTestKey
|
||||
error:&error];
|
||||
|
||||
XCTAssertNil(error);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue