Authored by Bogdan Poplauschi

Improvement for #1608 and #1623 should solve the issue for most of the cases. Ap…

…parently there is a race condition on NSURLCache and we avoid making those checks unless necesarry (basically we will query the NSURLCache only when `SDWebImageRefreshCached` is used and the image cannot be cached by the system when it's too big or behind authentication)
@@ -404,12 +404,14 @@ didReceiveResponse:(NSURLResponse *)response @@ -404,12 +404,14 @@ didReceiveResponse:(NSURLResponse *)response
404 } else { 404 } else {
405 SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock; 405 SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;
406 406
407 - if (![[NSURLCache sharedURLCache] cachedResponseForRequest:self.request]) {  
408 - responseFromCached = NO;  
409 - }  
410 -  
411 if (completionBlock) { 407 if (completionBlock) {
412 - if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) { 408 + /**
  409 + * See #1608 and #1623 - apparently, there is a race condition on `NSURLCache` that causes a crash
  410 + * Limited the calls to `cachedResponseForRequest:` only for cases where we should ignore the cached response
  411 + * and images for which responseFromCached is YES (only the ones that cannot be cached).
  412 + * Note: responseFromCached is set to NO inside `willCacheResponse:`. This method doesn't get called for large images or images behind authentication
  413 + */
  414 + if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached && [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request]) {
413 completionBlock(nil, nil, nil, YES); 415 completionBlock(nil, nil, nil, YES);
414 } else if (self.imageData) { 416 } else if (self.imageData) {
415 UIImage *image = [UIImage sd_imageWithData:self.imageData]; 417 UIImage *image = [UIImage sd_imageWithData:self.imageData];