Authored by Bogdan Poplauschi
Committed by GitHub

Merge pull request #2032 from dreampiggy/adopt_refresh_cache_behavior

Adopt the previous version behavior about SDWebImageDownloaderIgnoreC…
@@ -23,7 +23,6 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) { @@ -23,7 +23,6 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
23 /** 23 /**
24 * Call completion block with nil image/imageData if the image was read from NSURLCache 24 * Call completion block with nil image/imageData if the image was read from NSURLCache
25 * (to be combined with `SDWebImageDownloaderUseNSURLCache`). 25 * (to be combined with `SDWebImageDownloaderUseNSURLCache`).
26 - * I think this option should be renamed to 'SDWebImageDownloaderUsingCachedResponseDontLoad'  
27 */ 26 */
28 SDWebImageDownloaderIgnoreCachedResponse = 1 << 3, 27 SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
29 28
@@ -30,6 +30,7 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary; @@ -30,6 +30,7 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
30 @property (assign, nonatomic, getter = isExecuting) BOOL executing; 30 @property (assign, nonatomic, getter = isExecuting) BOOL executing;
31 @property (assign, nonatomic, getter = isFinished) BOOL finished; 31 @property (assign, nonatomic, getter = isFinished) BOOL finished;
32 @property (strong, nonatomic, nullable) NSMutableData *imageData; 32 @property (strong, nonatomic, nullable) NSMutableData *imageData;
  33 +@property (copy, nonatomic, nullable) NSData *cachedData;
33 34
34 // This is weak because it is injected by whoever manages this session. If this gets nil-ed out, we won't be able to run 35 // This is weak because it is injected by whoever manages this session. If this gets nil-ed out, we won't be able to run
35 // the task associated with this operation 36 // the task associated with this operation
@@ -148,6 +149,14 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary; @@ -148,6 +149,14 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
148 }]; 149 }];
149 } 150 }
150 #endif 151 #endif
  152 + if (self.options & SDWebImageDownloaderIgnoreCachedResponse) {
  153 + // Grab the cached data for later check
  154 + NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request];
  155 + if (cachedResponse) {
  156 + self.cachedData = cachedResponse.data;
  157 + }
  158 + }
  159 +
151 NSURLSession *session = self.unownedSession; 160 NSURLSession *session = self.unownedSession;
152 if (!self.unownedSession) { 161 if (!self.unownedSession) {
153 NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; 162 NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
@@ -451,13 +460,22 @@ didReceiveResponse:(NSURLResponse *)response @@ -451,13 +460,22 @@ didReceiveResponse:(NSURLResponse *)response
451 if ([self callbacksForKey:kCompletedCallbackKey].count > 0) { 460 if ([self callbacksForKey:kCompletedCallbackKey].count > 0) {
452 /** 461 /**
453 * If you specified to use `NSURLCache`, then the response you get here is what you need. 462 * If you specified to use `NSURLCache`, then the response you get here is what you need.
454 - * if you specified to only use cached data via `SDWebImageDownloaderIgnoreCachedResponse`,  
455 - * the response data will be nil.  
456 - * So we don't need to check the cache option here, since the system will obey the cache option  
457 */ 463 */
458 NSData *imageData = [self.imageData copy]; 464 NSData *imageData = [self.imageData copy];
459 if (imageData) { 465 if (imageData) {
460 UIImage *image = [UIImage sd_imageWithData:imageData]; 466 UIImage *image = [UIImage sd_imageWithData:imageData];
  467 + /** if you specified to only use cached data via `SDWebImageDownloaderIgnoreCachedResponse`,
  468 + * then we should check if the cached data is equal to image data
  469 + */
  470 + if (self.options & SDWebImageDownloaderIgnoreCachedResponse) {
  471 + if (self.cachedData) {
  472 + if ([self.cachedData isEqualToData:imageData]) {
  473 + // call completion block with nil
  474 + [self callCompletionBlocksWithImage:nil imageData:nil error:nil finished:YES];
  475 + return;
  476 + }
  477 + }
  478 + }
461 NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; 479 NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
462 image = [self scaledImageForKey:key image:image]; 480 image = [self scaledImageForKey:key image:image];
463 481