SDwebImage源码学习-下载篇

SDWebImageDownloaderOperation

@interface SDWebImageDownloaderOperation : NSOperation // 下面4个属性由SDWebImageDownloader传进来或者设置 @property (strong, nonatomic, readonly, nullable) NSURLRequest *request; // 请求 @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask; // 任务 @property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options; // 选择 @property (nonatomic, strong, nullable) NSURLCredential *credential; // 用于身份认证@property (assign, nonatomic) BOOL shouldDecompressImages; // 是否压缩图片,默认YES @property (assign, nonatomic) NSInteger expectedSize; // 预期的大小 @property (strong, nonatomic, nullable) NSURLResponse *response; // 响应// 初始化方法 - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request inSession:(nullable NSURLSession *)session options:(SDWebImageDownloaderOptions)options NS_DESIGNATED_INITIALIZER; // 添加进度和完成下载的回调,可以添加多个 // 返回的是NSMutableDictionary,用于取消任务,里面保存着进度和完成的block - (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock; // 取消任务,只要当callbackBlocks的个数为0,才是真正的取消请求 - (BOOL)cancel:(nullable id)token;

下载操作
// 从下面的代码可知: // 如果设置shouldContinueWhenAppEntersBackground = YES,就会开启一个后台任务 // 如果没有设置session,就创建自己的NSURLSession- (void)start { @synchronized (self) { if (self.isCancelled) { self.finished = YES; [self reset]; return; }#if SD_UIKIT Class UIApplicationClass = NSClassFromString(@"UIApplication"); BOOL hasApplication = UIApplicationClass && [UIApplicationClass respondsToSelector:@selector(sharedApplication)]; if (hasApplication && [self shouldContinueWhenAppEntersBackground]) { __weak __typeof__ (self) wself = self; UIApplication * app = [UIApplicationClass performSelector:@selector(sharedApplication)]; self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{ __strong __typeof (wself) sself = wself; if (sself) { [sself cancel]; [app endBackgroundTask:sself.backgroundTaskId]; sself.backgroundTaskId = UIBackgroundTaskInvalid; } }]; } #endif NSURLSession *session = self.unownedSession; if (!self.unownedSession) { NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfig.timeoutIntervalForRequest = 15; /** *Create the session for this task *We send nil as delegate queue so that the session creates a serial operation queue for performing all delegate *method calls and completion handler calls. */ self.ownedSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; session = self.ownedSession; }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 = https://www.it610.com/article/cachedResponse.data; } }self.dataTask = [session dataTaskWithRequest:self.request]; self.executing = YES; }[self.dataTask resume]; if (self.dataTask) { for (SDWebImageDownloaderProgressBlock progressBlock in [self callbacksForKey:kProgressCallbackKey]) { progressBlock(0, NSURLResponseUnknownLength, self.request.URL); } __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:weakSelf]; }); } else { [self callCompletionBlocksWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Task can't be initialized"}]]; }#if SD_UIKIT Class UIApplicationClass = NSClassFromString(@"UIApplication"); if(!UIApplicationClass || ![UIApplicationClass respondsToSelector:@selector(sharedApplication)]) { return; } if (self.backgroundTaskId != UIBackgroundTaskInvalid) { UIApplication * app = [UIApplication performSelector:@selector(sharedApplication)]; [app endBackgroundTask:self.backgroundTaskId]; self.backgroundTaskId = UIBackgroundTaskInvalid; } #endif }

下载回调处理
// 通过下面的代码可知: // 把每一次接收到的数据进行拼接, // 如果设置了SDWebImageDownloaderProgressiveDownload,就会调用CompletionBlocks,就可以看到图片有进度的下载- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { if (!self.imageData) { self.imageData = [[NSMutableData alloc] initWithCapacity:self.expectedSize]; } [self.imageData appendData:data]; if ((self.options & SDWebImageDownloaderProgressiveDownload) && self.expectedSize > 0) { // Get the image data NSData *imageData = https://www.it610.com/article/[self.imageData copy]; // Get the total bytes downloaded const NSInteger totalSize = imageData.length; // Get the finish status BOOL finished = (totalSize>= self.expectedSize); if (!self.progressiveCoder) { // We need to create a new instance for progressive decoding to avoid conflicts for (idcoder in [SDWebImageCodersManager sharedInstance].coders) { if ([coder conformsToProtocol:@protocol(SDWebImageProgressiveCoder)] && [((id)coder) canIncrementallyDecodeFromData:imageData]) { self.progressiveCoder = [[[coder class] alloc] init]; break; } } }UIImage *image = [self.progressiveCoder incrementallyDecodedImageWithData:imageData finished:finished]; if (image) { NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; image = [self scaledImageForKey:key image:image]; if (self.shouldDecompressImages) { image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}]; }[self callCompletionBlocksWithImage:image imageData:nil error:nil finished:NO]; } }for (SDWebImageDownloaderProgressBlock progressBlock in [self callbacksForKey:kProgressCallbackKey]) { progressBlock(self.imageData.length, self.expectedSize, self.request.URL); } }

取消操作
// 从下面的代码可知 // 只有当callbackBlocks的个数为0才是真正的取消请求,否则就是删除该回调而已 - (BOOL)cancel:(nullable id)token { __block BOOL shouldCancel = NO; dispatch_barrier_sync(self.barrierQueue, ^{ [self.callbackBlocks removeObjectIdenticalTo:token]; if (self.callbackBlocks.count == 0) { shouldCancel = YES; } }); if (shouldCancel) { [self cancel]; } return shouldCancel; }- (void)cancel { @synchronized (self) { [self cancelInternal]; } }- (void)cancelInternal { if (self.isFinished) return; [super cancel]; if (self.dataTask) { [self.dataTask cancel]; __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:weakSelf]; }); // As we cancelled the task, its callback won't be called and thus won't // maintain the isFinished and isExecuting flags. if (self.isExecuting) self.executing = NO; if (!self.isFinished) self.finished = YES; }[self reset]; }

SDWebImageDownloader
@interface SDWebImageDownloader : NSObject@property (assign, nonatomic)BOOL shouldDecompressImages; // 压缩图片 ,默认YES @property (assign, nonatomic)NSInteger maxConcurrentDownloads; // 最多允许多少个任务同时下载,默认6 @property (readonly, nonatomic) NSUInteger currentDownloadCount; // 有多少任务正在下载 @property (assign, nonatomic) NSTimeInterval downloadTimeout; // 超时,默认15 @property (readonly, nonatomic, nonnull) NSURLSessionConfiguration *sessionConfiguration; @property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder; // FIFO或者LIFO 默认FIFOE @property (nonatomic, copy, nullable) SDWebImageDownloaderHeadersFilterBlock headersFilter; // 用于外面对HTTPHeader做修改和过滤// 身份认证 @property (strong, nonatomic, nullable) NSURLCredential *urlCredential; @property (strong, nonatomic, nullable) NSString *username; @property (strong, nonatomic, nullable) NSString *password; // 获取单例、初始化 + (nonnull instancetype)sharedDownloader; - (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)sessionConfiguration NS_DESIGNATED_INITIALIZER; // HTTPHeader - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field; - (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field; // 用来自定义下载操作,operationClass必须继承NSOperation和遵守SDWebImageDownloaderOperationInterface - (void)setOperationClass:(nullable Class)operationClass; // 下载方法 - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock; // 设置是否暂停 - (void)setSuspended:(BOOL)suspended; // 取消 - (void)cancel:(nullable SDWebImageDownloadToken *)token; - (void)cancelAllDownloads; // 重新创建sesseion - (void)createNewSessionWithConfiguration:(nonnull NSURLSessionConfiguration *)sessionConfiguration; // 使session无效 - (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations;

下载
// 通过下面的代码可知: // 先去URLOperations字典中找是否有对应URL的操作 // 如果没有找到就创建一个Operation并设置操作完成之后从URLOperations中移除,并设置对应的token,用于取消操作// 创建Operation // - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock { __weak SDWebImageDownloader *wself = self; return [self addProgressCallback:progressBlock completedBlock:completedBlock forURL:url createCallback:^SDWebImageDownloaderOperation *{ __strong __typeof (wself) sself = wself; NSTimeInterval timeoutInterval = sself.downloadTimeout; if (timeoutInterval == 0.0) { timeoutInterval = 15.0; }// 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; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeoutInterval]; request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies); request.HTTPShouldUsePipelining = YES; if (sself.headersFilter) { request.allHTTPHeaderFields = sself.headersFilter(url, [sself allHTTPHeaderFields]); } else { request.allHTTPHeaderFields = [sself allHTTPHeaderFields]; } SDWebImageDownloaderOperation *operation = [[sself.operationClass alloc] initWithRequest:request inSession:sself.session options:options]; operation.shouldDecompressImages = sself.shouldDecompressImages; if (sself.urlCredential) { operation.credential = sself.urlCredential; } else if (sself.username && sself.password) { operation.credential = [NSURLCredential credentialWithUser:sself.username password:sself.password persistence:NSURLCredentialPersistenceForSession]; }if (options & SDWebImageDownloaderHighPriority) { operation.queuePriority = NSOperationQueuePriorityHigh; } else if (options & SDWebImageDownloaderLowPriority) { operation.queuePriority = NSOperationQueuePriorityLow; }[sself.downloadQueue addOperation:operation]; if (sself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) { // Emulate LIFO execution order by systematically adding new operations as last operation's dependency [sself.lastAddedOperation addDependency:operation]; sself.lastAddedOperation = operation; }return operation; }]; }- (nullable SDWebImageDownloadToken *)addProgressCallback:(SDWebImageDownloaderProgressBlock)progressBlock completedBlock:(SDWebImageDownloaderCompletedBlock)completedBlock forURL:(nullable NSURL *)url createCallback:(SDWebImageDownloaderOperation *(^)(void))createCallback { // The URL will be used as the key to the callbacks dictionary so it cannot be nil. If it is nil immediately call the completed block with no image or data. if (url == nil) { if (completedBlock != nil) { completedBlock(nil, nil, nil, NO); } return nil; }LOCK(self.operationsLock); SDWebImageDownloaderOperation *operation = [self.URLOperations objectForKey:url]; if (!operation) { operation = createCallback(); __weak typeof(self) wself = self; operation.completionBlock = ^{ __strong typeof(wself) sself = wself; if (!sself) { return; } LOCK(sself.operationsLock); [sself.URLOperations removeObjectForKey:url]; UNLOCK(sself.operationsLock); }; [self.URLOperations setObject:operation forKey:url]; } UNLOCK(self.operationsLock); id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock]; SDWebImageDownloadToken *token = [SDWebImageDownloadToken new]; token.downloadOperation = operation; token.url = url; token.downloadOperationCancelToken = downloadOperationCancelToken; return token; }

下载回调
// 通过下面的代码可知: // session的delegate是SDWebImageDownloader,然后根据task.taskIdentifier找出对应的对应的SDWebImageDownloaderOperation // sesseion的delgate只能有一个,但是operation有多个,回调要回调给对应operation。所以需要找出对应task,并调用对应的方法//self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil]; - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {SDWebImageDownloaderOperation *dataOperation = [self operationWithTask:task]; if ([dataOperation respondsToSelector:@selector(URLSession:task:didCompleteWithError:)]) { [dataOperation URLSession:session task:task didCompleteWithError:error]; } }- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler {SDWebImageDownloaderOperation *dataOperation = [self operationWithTask:task]; if ([dataOperation respondsToSelector:@selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:)]) { [dataOperation URLSession:session task:task willPerformHTTPRedirection:response newRequest:request completionHandler:completionHandler]; } else { if (completionHandler) { completionHandler(request); } } }- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {SDWebImageDownloaderOperation *dataOperation = [self operationWithTask:task]; if ([dataOperation respondsToSelector:@selector(URLSession:task:didReceiveChallenge:completionHandler:)]) { [dataOperation URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler]; } else { if (completionHandler) { completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); } } }

取消和暂停
// 通过下面的代码可看出 // 暂停和取消全部任务直接操作Queue就可以- (void)setSuspended:(BOOL)suspended { self.downloadQueue.suspended = suspended; } - (void)cancelAllDownloads { [self.downloadQueue cancelAllOperations]; }// 取消单个任务,需要从URLOperations移除该操作 - (void)cancel:(nullable SDWebImageDownloadToken *)token { NSURL *url = token.url; if (!url) { return; } LOCK(self.operationsLock); SDWebImageDownloaderOperation *operation = [self.URLOperations objectForKey:url]; if (operation) { BOOL canceled = [operation cancel:token.downloadOperationCancelToken]; if (canceled) { [self.URLOperations removeObjectForKey:url]; } } UNLOCK(self.operationsLock); }

学到的技术
// 身份认证 - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ //以前的失败次数 if ([challenge previousFailureCount] == 0) { //身份认证的类 NSURLCredential *newCredential; newCredential = [NSURLCredential credentialWithUser:@"账号" password:@"密码" persistence:NSURLCredentialPersistenceNone]; [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; }else{ [[challenge sender] cancelAuthenticationChallenge:challenge]; } }

// 加锁 #define LOCK(lock) dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER); #define UNLOCK(lock) dispatch_semaphore_signal(lock); @property (strong, nonatomic, nonnull) dispatch_semaphore_t headersLock; LOCK(self.headersLock); SDHTTPHeadersDictionary *allHTTPHeaderFields = [self.HTTPHeaders copy]; UNLOCK(self.headersLock);

// OperationQueue实现LIFO [sself.downloadQueue addOperation:operation]; if (sself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) { [sself.lastAddedOperation addDependency:operation]; sself.lastAddedOperation = operation; }

    推荐阅读