Authored by Bogdan Poplauschi

Updated `queryCacheOperationForKey:image:` comments and method still works if the doneBlock is nil

@@ -146,11 +146,13 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot @@ -146,11 +146,13 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot
146 */ 146 */
147 - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key; 147 - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key;
148 148
  149 +#pragma mark - Query Ops
  150 +
149 /** 151 /**
150 - * Query the cache asynchronously and call the completion when done. 152 + * Operation that queries the cache asynchronously and call the completion when done.
151 * 153 *
152 * @param key The unique key used to store the wanted image 154 * @param key The unique key used to store the wanted image
153 - * @param doneBlock The completion block 155 + * @param doneBlock The completion block. Will not get called if the operation is cancelled
154 * 156 *
155 * @return a NSOperation instance containing the cache op 157 * @return a NSOperation instance containing the cache op
156 */ 158 */
@@ -275,6 +275,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -275,6 +275,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
275 }); 275 });
276 } 276 }
277 277
  278 +#pragma mark - Query Ops
  279 +
278 - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key { 280 - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key {
279 return [self.memCache objectForKey:key]; 281 return [self.memCache objectForKey:key];
280 } 282 }
@@ -354,12 +356,10 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -354,12 +356,10 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
354 } 356 }
355 357
356 - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock { 358 - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock {
357 - if (!doneBlock) {  
358 - return nil;  
359 - }  
360 -  
361 if (!key) { 359 if (!key) {
362 - doneBlock(nil, nil, SDImageCacheTypeNone); 360 + if (doneBlock) {
  361 + doneBlock(nil, nil, SDImageCacheTypeNone);
  362 + }
363 return nil; 363 return nil;
364 } 364 }
365 365
@@ -370,13 +370,16 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -370,13 +370,16 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
370 if ([image isGIF]) { 370 if ([image isGIF]) {
371 diskData = [self diskImageDataBySearchingAllPathsForKey:key]; 371 diskData = [self diskImageDataBySearchingAllPathsForKey:key];
372 } 372 }
373 - doneBlock(image, diskData, SDImageCacheTypeMemory); 373 + if (doneBlock) {
  374 + doneBlock(image, diskData, SDImageCacheTypeMemory);
  375 + }
374 return nil; 376 return nil;
375 } 377 }
376 378
377 NSOperation *operation = [NSOperation new]; 379 NSOperation *operation = [NSOperation new];
378 dispatch_async(self.ioQueue, ^{ 380 dispatch_async(self.ioQueue, ^{
379 if (operation.isCancelled) { 381 if (operation.isCancelled) {
  382 + // do not call the completion if cancelled
380 return; 383 return;
381 } 384 }
382 385
@@ -388,9 +391,11 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -388,9 +391,11 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
388 [self.memCache setObject:diskImage forKey:key cost:cost]; 391 [self.memCache setObject:diskImage forKey:key cost:cost];
389 } 392 }
390 393
391 - dispatch_async(dispatch_get_main_queue(), ^{  
392 - doneBlock(diskImage, diskData, SDImageCacheTypeDisk);  
393 - }); 394 + if (doneBlock) {
  395 + dispatch_async(dispatch_get_main_queue(), ^{
  396 + doneBlock(diskImage, diskData, SDImageCacheTypeDisk);
  397 + });
  398 + }
394 } 399 }
395 }); 400 });
396 401