Authored by Bogdan Poplauschi

Code review and cleanup for #1737

... ... @@ -25,13 +25,12 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
* (to be combined with `SDWebImageDownloaderUseNSURLCache`).
* I think this option should be renamed to 'SDWebImageDownloaderUsingCachedResponseDontLoad'
*/
SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
/**
* In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
* extra time in background to let the request finish. If the background task expires the operation will be cancelled.
*/
SDWebImageDownloaderContinueInBackground = 1 << 4,
/**
... ...
... ... @@ -153,16 +153,17 @@
}
// 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:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeoutInterval];
NSURLRequestCachePolicy cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
if (options & SDWebImageDownloaderUseNSURLCache) {
if (options & SDWebImageDownloaderIgnoreCachedResponse) {
request.cachePolicy = NSURLRequestReturnCacheDataDontLoad;
}
else {
request.cachePolicy = NSURLRequestUseProtocolCachePolicy;
cachePolicy = NSURLRequestReturnCacheDataDontLoad;
} else {
cachePolicy = NSURLRequestUseProtocolCachePolicy;
}
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeoutInterval];
request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);
request.HTTPShouldUsePipelining = YES;
if (sself.headersFilter) {
... ...
... ... @@ -52,8 +52,6 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
#if SD_UIKIT || SD_WATCH
UIImageOrientation orientation;
#endif
//useless now
// BOOL responseFromCached;
}
@synthesize executing = _executing;
... ... @@ -75,7 +73,6 @@ 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
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderOperationBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
}
return self;
... ... @@ -387,8 +384,7 @@ didReceiveResponse:(NSURLResponse *)response
dataTask:(NSURLSessionDataTask *)dataTask
willCacheResponse:(NSCachedURLResponse *)proposedResponse
completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler {
// responseFromCached = NO; // If this method is called, it means the response wasn't read from cache
NSCachedURLResponse *cachedResponse = proposedResponse;
if (self.request.cachePolicy == NSURLRequestReloadIgnoringLocalCacheData) {
... ... @@ -418,17 +414,10 @@ didReceiveResponse:(NSURLResponse *)response
} else {
if ([self callbacksForKey:kCompletedCallbackKey].count > 0) {
/**
* See #1608 and #1623 - apparently, there is a race condition on `NSURLCache` that causes a crash
* Limited the calls to `cachedResponseForRequest:` only for cases where we should ignore the cached 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 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 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
*/
if (self.imageData) {
UIImage *image = [UIImage sd_imageWithData:self.imageData];
... ...