diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index c5da963f..cf87c62f 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -146,11 +146,13 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot */ - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key; +#pragma mark - Query Ops + /** - * Query the cache asynchronously and call the completion when done. + * Operation that queries the cache asynchronously and call the completion when done. * * @param key The unique key used to store the wanted image - * @param doneBlock The completion block + * @param doneBlock The completion block. Will not get called if the operation is cancelled * * @return a NSOperation instance containing the cache op */ diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 2965784f..97b07a03 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -275,6 +275,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { }); } +#pragma mark - Query Ops + - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key { return [self.memCache objectForKey:key]; } @@ -354,12 +356,10 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock { - if (!doneBlock) { - return nil; - } - if (!key) { - doneBlock(nil, nil, SDImageCacheTypeNone); + if (doneBlock) { + doneBlock(nil, nil, SDImageCacheTypeNone); + } return nil; } @@ -370,13 +370,16 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { if ([image isGIF]) { diskData = [self diskImageDataBySearchingAllPathsForKey:key]; } - doneBlock(image, diskData, SDImageCacheTypeMemory); + if (doneBlock) { + doneBlock(image, diskData, SDImageCacheTypeMemory); + } return nil; } NSOperation *operation = [NSOperation new]; dispatch_async(self.ioQueue, ^{ if (operation.isCancelled) { + // do not call the completion if cancelled return; } @@ -388,9 +391,11 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { [self.memCache setObject:diskImage forKey:key cost:cost]; } - dispatch_async(dispatch_get_main_queue(), ^{ - doneBlock(diskImage, diskData, SDImageCacheTypeDisk); - }); + if (doneBlock) { + dispatch_async(dispatch_get_main_queue(), ^{ + doneBlock(diskImage, diskData, SDImageCacheTypeDisk); + }); + } } });