美文网首页
SDWebImage-如何处理NSURLCache

SDWebImage-如何处理NSURLCache

作者: Code_人生 | 来源:发表于2019-09-30 14:00 被阅读0次
    1、来到SDWebImageDownloader.hSDWebImageDownloaderOptions
    2、全局搜索SDWebImageDownloaderUseNSURLCache

    2.1、用户设置了SDWebImageDownloaderUseNSURLCache缓存策略,使用默认缓存NSURLRequestUseProtocolCachePolicy;没设置,忽略缓存NSURLRequestReloadIgnoringLocalCacheData,避免NSURLCache再缓存一次。

        NSURLRequestCachePolicy cachePolicy = options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData;
    
        if (options & SDWebImageRefreshCached) downloaderOptions |= SDWebImageDownloaderUseNSURLCache;
    

    2.3、cachedResponse置为nil,避免进行本地缓存

    - (void)URLSession:(NSURLSession *)session
              dataTask:(NSURLSessionDataTask *)dataTask
     willCacheResponse:(NSCachedURLResponse *)proposedResponse
     completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler {
        
        NSCachedURLResponse *cachedResponse = proposedResponse;
    
        if (!(self.options & SDWebImageDownloaderUseNSURLCache)) {
            // Prevents caching of responses
            cachedResponse = nil;
        }
        if (completionHandler) {
            completionHandler(cachedResponse);
        }
    }
    
    3、全局搜索SDWebImageDownloaderUseNSURLCache
        if (cachedImage && options & SDWebImageRefreshCached) {
            // force progressive off if image already cached but forced refreshing
            downloaderOptions &= ~SDWebImageDownloaderProgressiveLoad;
            // ignore image read from NSURLCache if image if cached but force refreshing
            downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
        }
    
            if (self.options & SDWebImageDownloaderIgnoreCachedResponse) {
                // Grab the cached data for later check
                NSURLCache *URLCache = session.configuration.URLCache;
                if (!URLCache) {
                    URLCache = [NSURLCache sharedURLCache];
                }
                NSCachedURLResponse *cachedResponse;
                // NSURLCache's `cachedResponseForRequest:` is not thread-safe, see https://developer.apple.com/documentation/foundation/nsurlcache#2317483
                @synchronized (URLCache) {
                    cachedResponse = [URLCache cachedResponseForRequest:self.request];
                }
                if (cachedResponse) {
                    self.cachedData = cachedResponse.data;
                }
            }
    
                    /**  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 && [self.cachedData isEqualToData:imageData]) {
                        self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorCacheNotModified userInfo:nil];
                        // call completion block with not modified error
                        [self callCompletionBlocksWithError:self.responseError];
                        [self done];
                    } else {
                        // decode the image in coder queue
                        dispatch_async(self.coderQueue, ^{
                            @autoreleasepool {
                                UIImage *image = SDImageLoaderDecodeImageData(imageData, self.request.URL, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
                                CGSize imageSize = image.size;
                                if (imageSize.width == 0 || imageSize.height == 0) {
                                    [self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorBadImageData userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}]];
                                } else {
                                    [self callCompletionBlocksWithImage:image imageData:imageData error:nil finished:YES];
                                }
                                [self done];
                            }
                        });
                    }
    
    • SDWebImageErrorCacheNotModified 错误码
        if (statusCode == 304 && !self.cachedData) {
            valid = NO;
            self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorCacheNotModified userInfo:nil];
        }
    

    相关文章

      网友评论

          本文标题:SDWebImage-如何处理NSURLCache

          本文链接:https://www.haomeiwen.com/subject/lcdsyctx.html