From ecdd0128ab3ef2e7f44d59efe6079172f4922504 Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Fri, 11 Aug 2017 16:37:29 +0300 Subject: [PATCH 1/4] Fixed #1993 Load image with option `SDWebImageRefreshCached` get error #1737 introduced an issue that is using NSURLRequestReturnCacheDataDontLoad for our cached images which makes us never download again, which is not ok when using `SDWebImageRefreshCached`. Fixed by reverting to the original implementation here: `NSURLRequestUseProtocolCachePolicy` vs `NSURLRequestReloadIgnoringLocalCacheData` (when `SDWebImageRefreshCached` is set) --- SDWebImage/SDWebImageDownloader.m | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 29e7d59d..2f2198e8 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -166,16 +166,10 @@ } // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise - NSURLRequestCachePolicy cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; - if (options & SDWebImageDownloaderUseNSURLCache) { - if (options & SDWebImageDownloaderIgnoreCachedResponse) { - cachePolicy = NSURLRequestReturnCacheDataDontLoad; - } else { - cachePolicy = NSURLRequestUseProtocolCachePolicy; - } - } - - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeoutInterval]; + NSURLRequestCachePolicy cachePolicy = options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData; + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url + cachePolicy:cachePolicy + timeoutInterval:timeoutInterval]; request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies); request.HTTPShouldUsePipelining = YES; From 7b79c29a8cd03c3bb50a1a86a2f3b0113bee023e Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Fri, 11 Aug 2017 16:38:32 +0300 Subject: [PATCH 2/4] The docs say the progressBlock is called on a background queue, so when doing UI, we need to dispatch on the main queue * @param progressBlock A block called while image is downloading * @note the progress block is executed on a background queue --- Examples/SDWebImage Demo/DetailViewController.m | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Examples/SDWebImage Demo/DetailViewController.m b/Examples/SDWebImage Demo/DetailViewController.m index 0d294bf6..eef1ca11 100644 --- a/Examples/SDWebImage Demo/DetailViewController.m +++ b/Examples/SDWebImage Demo/DetailViewController.m @@ -43,11 +43,13 @@ placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL *targetURL) { - if (!activityIndicator) { - [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]]; - activityIndicator.center = weakImageView.center; - [activityIndicator startAnimating]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + if (!activityIndicator) { + [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]]; + activityIndicator.center = weakImageView.center; + [activityIndicator startAnimating]; + } + }); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { [activityIndicator removeFromSuperview]; From ae75b747b039ff3650e9883fd291017133218aaa Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Fri, 11 Aug 2017 17:18:31 +0300 Subject: [PATCH 3/4] Update for cache policy calculation: I think that if SDWebImageDownloaderIgnoreCachedResponse is there, we need NSURLRequestReloadIgnoringLocalCacheData so we always reload. --- SDWebImage/SDWebImageDownloader.m | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 2f2198e8..d1ac8bb8 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -166,7 +166,14 @@ } // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise - NSURLRequestCachePolicy cachePolicy = options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData; + NSURLRequestCachePolicy cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; + if (options & SDWebImageDownloaderUseNSURLCache) { + if (options & SDWebImageDownloaderIgnoreCachedResponse) { + cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; + } else { + cachePolicy = NSURLRequestUseProtocolCachePolicy; + } + } NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeoutInterval]; From 79124e1bab17d0ecee411501ffa0266a8e020b3b Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Fri, 11 Aug 2017 18:00:59 +0300 Subject: [PATCH 4/4] Revert "Update for cache policy calculation: I think that if SDWebImageDownloaderIgnoreCachedResponse is there, we need NSURLRequestReloadIgnoringLocalCacheData so we always reload." This reverts commit ae75b747b039ff3650e9883fd291017133218aaa. --- SDWebImage/SDWebImageDownloader.m | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index d1ac8bb8..2f2198e8 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -166,14 +166,7 @@ } // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise - NSURLRequestCachePolicy cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; - if (options & SDWebImageDownloaderUseNSURLCache) { - if (options & SDWebImageDownloaderIgnoreCachedResponse) { - cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; - } else { - cachePolicy = NSURLRequestUseProtocolCachePolicy; - } - } + NSURLRequestCachePolicy cachePolicy = options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeoutInterval];