Adopt the previous version behavior about SDWebImageDownloaderIgnoreCachedResponse and SDWebImageRefreshCached without hack code

This commit is contained in:
DreamPiggy 2017-09-25 10:54:22 +08:00
parent 52993c7150
commit 7491510f7e
2 changed files with 21 additions and 4 deletions

View File

@ -23,7 +23,6 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
/** /**
* Call completion block with nil image/imageData if the image was read from NSURLCache * Call completion block with nil image/imageData if the image was read from NSURLCache
* (to be combined with `SDWebImageDownloaderUseNSURLCache`). * (to be combined with `SDWebImageDownloaderUseNSURLCache`).
* I think this option should be renamed to 'SDWebImageDownloaderUsingCachedResponseDontLoad'
*/ */
SDWebImageDownloaderIgnoreCachedResponse = 1 << 3, SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,

View File

@ -30,6 +30,7 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
@property (assign, nonatomic, getter = isExecuting) BOOL executing; @property (assign, nonatomic, getter = isExecuting) BOOL executing;
@property (assign, nonatomic, getter = isFinished) BOOL finished; @property (assign, nonatomic, getter = isFinished) BOOL finished;
@property (strong, nonatomic, nullable) NSMutableData *imageData; @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 // 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 // the task associated with this operation
@ -148,6 +149,14 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
}]; }];
} }
#endif #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; NSURLSession *session = self.unownedSession;
if (!self.unownedSession) { if (!self.unownedSession) {
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
@ -451,13 +460,22 @@ didReceiveResponse:(NSURLResponse *)response
if ([self callbacksForKey:kCompletedCallbackKey].count > 0) { 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 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]; NSData *imageData = [self.imageData copy];
if (imageData) { if (imageData) {
UIImage *image = [UIImage sd_imageWithData: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]; NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
image = [self scaledImageForKey:key image:image]; image = [self scaledImageForKey:key image:image];