From 7491510f7ec77009fd315bc673d21e9447243ceb Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 25 Sep 2017 10:54:22 +0800 Subject: [PATCH] Adopt the previous version behavior about SDWebImageDownloaderIgnoreCachedResponse and SDWebImageRefreshCached without hack code --- SDWebImage/SDWebImageDownloader.h | 1 - SDWebImage/SDWebImageDownloaderOperation.m | 24 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.h b/SDWebImage/SDWebImageDownloader.h index cdfe7291..4e69fe87 100644 --- a/SDWebImage/SDWebImageDownloader.h +++ b/SDWebImage/SDWebImageDownloader.h @@ -23,7 +23,6 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) { /** * Call completion block with nil image/imageData if the image was read from NSURLCache * (to be combined with `SDWebImageDownloaderUseNSURLCache`). - * I think this option should be renamed to 'SDWebImageDownloaderUsingCachedResponseDontLoad' */ SDWebImageDownloaderIgnoreCachedResponse = 1 << 3, diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index c35ea1a8..a9918c31 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -30,6 +30,7 @@ typedef NSMutableDictionary SDCallbacksDictionary; @property (assign, nonatomic, getter = isExecuting) BOOL executing; @property (assign, nonatomic, getter = isFinished) BOOL finished; @property (strong, nonatomic, nullable) NSMutableData *imageData; +@property (copy, nonatomic, nullable) NSData *cachedData; // 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 // the task associated with this operation @@ -148,6 +149,14 @@ typedef NSMutableDictionary SDCallbacksDictionary; }]; } #endif + if (self.options & SDWebImageDownloaderIgnoreCachedResponse) { + // Grab the cached data for later check + NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request]; + if (cachedResponse) { + self.cachedData = cachedResponse.data; + } + } + NSURLSession *session = self.unownedSession; if (!self.unownedSession) { NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; @@ -451,13 +460,22 @@ didReceiveResponse:(NSURLResponse *)response if ([self callbacksForKey:kCompletedCallbackKey].count > 0) { /** * If you specified to use `NSURLCache`, then the response you get here is what you need. - * if you specified to only use cached data via `SDWebImageDownloaderIgnoreCachedResponse`, - * the response data will be nil. - * So we don't need to check the cache option here, since the system will obey the cache option */ NSData *imageData = [self.imageData copy]; if (imageData) { UIImage *image = [UIImage sd_imageWithData:imageData]; + /** if you specified to only use cached data via `SDWebImageDownloaderIgnoreCachedResponse`, + * then we should check if the cached data is equal to image data + */ + if (self.options & SDWebImageDownloaderIgnoreCachedResponse) { + if (self.cachedData) { + if ([self.cachedData isEqualToData:imageData]) { + // call completion block with nil + [self callCompletionBlocksWithImage:nil imageData:nil error:nil finished:YES]; + return; + } + } + } NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; image = [self scaledImageForKey:key image:image];