clear image downloading cache policy to fix issue #1623

This commit is contained in:
zhang 2016-12-01 11:21:04 +08:00
parent 032ab1dd6d
commit 1b954b010b
3 changed files with 28 additions and 9 deletions

View File

@ -23,6 +23,7 @@ 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,

View File

@ -153,7 +153,16 @@
}
// In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:(options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:timeoutInterval];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeoutInterval];
if (options & SDWebImageDownloaderUseNSURLCache) {
if (options & SDWebImageDownloaderIgnoreCachedResponse) {
request.cachePolicy = NSURLRequestReturnCacheDataDontLoad;
}
else {
request.cachePolicy = NSURLRequestUseProtocolCachePolicy;
}
}
request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);
request.HTTPShouldUsePipelining = YES;
if (sself.headersFilter) {

View File

@ -52,7 +52,8 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
#if SD_UIKIT || SD_WATCH
UIImageOrientation orientation;
#endif
BOOL responseFromCached;
//useless now
// BOOL responseFromCached;
}
@synthesize executing = _executing;
@ -74,7 +75,7 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
_finished = NO;
_expectedSize = 0;
_unownedSession = session;
responseFromCached = YES; // Initially wrong until `- URLSession:dataTask:willCacheResponse:completionHandler: is called or not called
// responseFromCached = YES; // Initially wrong until `- URLSession:dataTask:willCacheResponse:completionHandler: is called or not called
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderOperationBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
}
return self;
@ -387,7 +388,7 @@ didReceiveResponse:(NSURLResponse *)response
willCacheResponse:(NSCachedURLResponse *)proposedResponse
completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler {
responseFromCached = NO; // If this method is called, it means the response wasn't read from cache
// responseFromCached = NO; // If this method is called, it means the response wasn't read from cache
NSCachedURLResponse *cachedResponse = proposedResponse;
if (self.request.cachePolicy == NSURLRequestReloadIgnoringLocalCacheData) {
@ -422,10 +423,18 @@ didReceiveResponse:(NSURLResponse *)response
* and images for which responseFromCached is YES (only the ones that cannot be cached).
* Note: responseFromCached is set to NO inside `willCacheResponse:`. This method doesn't get called for large images or images behind authentication
*/
if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached && [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request]) {
// hack
[self callCompletionBlocksWithImage:nil imageData:nil error:nil finished:YES];
} else if (self.imageData) {
/**
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`(This name is confusing),
the response data will be nil.
So we don't need to check the cache option here, because we have set the cache option of the request already, and system will obey it.
*/
// if (self.options & SDWebImageDownloaderUseNSURLCache) {
// // hack
// [self callCompletionBlocksWithImage:nil imageData:nil error:nil finished:YES];
// } else if (self.imageData) {
UIImage *image = [UIImage sd_imageWithData:self.imageData];
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
image = [self scaledImageForKey:key image:image];
@ -451,7 +460,7 @@ didReceiveResponse:(NSURLResponse *)response
} else {
[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Image data is nil"}]];
}
}
// }
}
[self done];
}