前言
通过前两篇关于SDWebImage的学习,我们已经知道了它的下载策略和缓存策略。本次要学习的内容是SDWebImage中是通过怎样的调度方法来使用下载和缓存的。
方法的调用
在使用的时候,我们通常会直接调用类别中提供的方法来加载图片,查看它们的实现我们可以发现最终我们会调用到这样一个函数
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
operationKey:(nullable NSString *)operationKey
setImageBlock:(nullable SDSetImageBlock)setImageBlock
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDExternalCompletionBlock)completedBlock
context:(nullable NSDictionary *)context;
外部提供的接口都是对这个函数的一个封装,方便我们使用时候调用。来看看具体的实现:
里面首先对operationKey进行了检验,生成可用的key,并将这个key对应的operation添加到了一个SDOperationsDictionary类的字典中,并绑定到对应的view中。后续在管理任务都会用到这个字典。
NSString *validOperationKey = operationKey ?: NSStringFromClass([self class]);
[self sd_cancelImageLoadOperationWithKey:validOperationKey];
objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
省略一些检测代码,当URL正确时,就会调用SDWebImageManager类的单利对象进行图片下载。这个方法就会返回一个operation回来,通过[self sd_setImageLoadOperation:operation forKey:validOperationKey];
将这个operation保存到之前说到的字典中。
id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
/**
省略若干代码,这些代码的作用是根据用户的设定将下载好的数据返回给上层。
*//
[self sd_setImageLoadOperation:operation forKey:validOperationKey];
}
可以发现,这个函数中并没有下载和缓存相关的代码,于是我们可以猜测下载和缓存相关的逻辑是放在SDWebImageManager类中进行处理的。下面我们就对调度类SDWebImageManager进行进一步的查看分析。
SDWebImageManager
查看SDWebImageManager.h文件,可以发现SDWebImageManager中提供了2个我们之前讲到过的属性,分别是SDWebImageDownloader
和imageCache
。这两个实例中不就是提供下载和缓存的操作吗?
@interface SDWebImageManager : NSObject
@property (weak, nonatomic, nullable) id <SDWebImageManagerDelegate> delegate;
@property (strong, nonatomic, readonly, nullable) SDImageCache *imageCache;
@property (strong, nonatomic, readonly, nullable) SDWebImageDownloader *imageDownloader;
进一步查看,还提供了一个遵循<SDWebImageManagerDelegate>
协议的代理。<SDWebImageManagerDelegate>
协议主要提供了2个接口:
//控制在cache中没有找到image时 是否应该去下载。默认是YES。
- (BOOL)imageManager:(nonnull SDWebImageManager *)imageManager shouldDownloadImageForURL:(nullable NSURL *)imageURL;
//在下载之后,缓存之前转换图片。在全局队列中操作,不阻塞主线程
- (nullable UIImage *)imageManager:(nonnull SDWebImageManager *)imageManager transformDownloadedImage:(nullable UIImage *)image withURL:(nullable NSURL *)imageURL;
此外,SDWebImageManager还提供了这些操作,包括图片的下载,取消,缓存,检测等。我们需要重点学习的方法是downloadImageWithURL
下载这个函数。
- (instancetype)initWithCache:(SDImageCache *)cache downloader:(SDWebImageDownloader *)downloader;
//下载图片
- (id )downloadImageWithURL:(NSURL *)url
options:(SDWebImageOptions)options
progress:(SDWebImageDownloaderProgressBlock)progressBlock
completed:(SDWebImageCompletionWithFinishedBlock)completedBlock;
//缓存给定URL的图片
- (void)saveImageToCache:(UIImage *)image forURL:(NSURL *)url;
//取消当前所有的操作
- (void)cancelAll;
//监测当前是否有进行中的操作
- (BOOL)isRunning;
//监测图片是否在缓存中,监测结束后调用completionBlock
- (void)cachedImageExistsForURL:(NSURL *)url
completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;
//监测图片是否缓存在disk里,监测结束后调用completionBlock
- (void)diskImageExistsForURL:(NSURL *)url
completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;
//返回给定URL的cache key,默认是图片的url
- (NSString *)cacheKeyForURL:(NSURL *)url;
对于downloadImageWithURL
下载函数来说,它需要传入4个参数,和返回SDWebImageOperation类的实例。
- @param url 网络图片的 url 地址
- @param options 一些定制化选项
- @param progressBlock 下载时的 Block,其定义为:typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize);
- @param completedBlock 下载完成时的 Block,其定义为:typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, NSError *error, BOOL finished);
- @return 返回 SDWebImageOperation 的实例
具体的流程是
1.验证URL正确性
如果不正确,则回调error
if ([url isKindOfClass:NSString.class]) {
url = [NSURL URLWithString:(NSString *)url];
}
// Prevents app crashing on argument type error like sending NSNull instead of NSURL
if (![url isKindOfClass:NSURL.class]) {
url = nil;
}
__block SDWebImageCombinedOperation *operation = [SDWebImageCombinedOperation new];
__weak SDWebImageCombinedOperation *weakOperation = operation;
BOOL isFailedUrl = NO;
if (url) {
@synchronized (self.failedURLs) {
isFailedUrl = [self.failedURLs containsObject:url];
}
}
if (url.absoluteString.length == 0 || (!(options & SDWebImageRetryFailed) && isFailedUrl)) {
[self callCompletionBlockForOperation:operation completion:completedBlock error:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil] url:url];
return operation;
}
2.检测该URL是否被下载过,去缓存中查找图片
私有成员变量是一个NSMutableArray<SDWebImageCombinedOperation *>的数组,用来保存所有正在进行的operation,方便统一控制。
@synchronized (self.runningOperations) {
[self.runningOperations addObject:operation];
}
//根据url生成缓存对应的key
NSString *key = [self cacheKeyForURL:url];
//将查找结果保存到operation中的chcheOperation中。
operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) {
if (operation.isCancelled) {
[self safelyRemoveOperationFromRunning:operation];
return;
}
3.根据不同的情况进行不同的操作
3.1>如果在缓存中没有找到图片,或者采用的 SDWebImageRefreshCached 选项,则从网络下载
这个操作会先调用imageDownloader
尝试下载图片,如果下载失败则抛出异常,如果下载成功则会调用imageCache
将图片进行缓存。
//如果没有缓存或者用户允许刷新缓存
if ((!cachedImage || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])) {
//如果在缓存中找到了图片,直接回调。
if (cachedImage && options & SDWebImageRefreshCached) {
[self callCompletionBlockForOperation:weakOperation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
}
/**
省略一部分用户定制策略的判断
**/
//使用imageDownloader进行下载。
SDWebImageDownloadToken *subOperationToken = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
__strong __typeof(weakOperation) strongOperation = weakOperation;
if (!strongOperation || strongOperation.isCancelled) {
//如果这个任务被取消了,则什么都不做
} else if (error) {
[self callCompletionBlockForOperation:strongOperation completion:completedBlock error:error url:url];
如果这个任务失败了,则组装error对象,抛出
if ( error.code != NSURLErrorNotConnectedToInternet
&& error.code != NSURLErrorCancelled
&& error.code != NSURLErrorTimedOut
&& error.code != NSURLErrorInternationalRoamingOff
&& error.code != NSURLErrorDataNotAllowed
&& error.code != NSURLErrorCannotFindHost
&& error.code != NSURLErrorCannotConnectToHost
&& error.code != NSURLErrorNetworkConnectionLost) {
@synchronized (self.failedURLs) {
//将这个URL添加到失败URL数组中
[self.failedURLs addObject:url];
}
}
}
else {
//下载成功,如果url曾经失败过,则将这个url从失败数组中移除
if ((options & SDWebImageRetryFailed)) {
@synchronized (self.failedURLs) {
[self.failedURLs removeObject:url];
}
}
BOOL cacheOnDisk = !(options & SDWebImageCacheMemoryOnly);
if (options & SDWebImageRefreshCached && cachedImage && !downloadedImage) {
//只是刷新缓存,则不回调
} else if (downloadedImage && (!downloadedImage.images || (options & SDWebImageTransformAnimatedImage)) && [self.delegate respondsToSelector:@selector(imageManager:transformDownloadedImage:withURL:)]) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//如果下载成功,并且不是GIF图片,并且代理实现了图片转换
//则先使用代理进行图片转换
//将装换后的结果进行缓存
UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url];
if (transformedImage && finished) {
BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage];
// pass nil if the image was transformed, so we can recalculate the data from the image
[self.imageCache storeImage:transformedImage imageData:(imageWasTransformed ? nil : downloadedData) forKey:key toDisk:cacheOnDisk completion:nil];
}
//将结果回调
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
});
} else {
if (downloadedImage && finished) {
//缓存
[self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil];
}
//回调
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:downloadedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
}
}
if (finished) {
//完毕后,将operation从进行中数组中移除
[self safelyRemoveOperationFromRunning:strongOperation];
}
}];
@synchronized(operation) {
// Need same lock to ensure cancelBlock called because cancel method can be called in different queue
operation.cancelBlock = ^{
[self.imageDownloader cancel:subOperationToken];
__strong __typeof(weakOperation) strongOperation = weakOperation;
[self safelyRemoveOperationFromRunning:strongOperation];
};
}
3.2>有缓存则直接返回
else if (cachedImage) {
__strong __typeof(weakOperation) strongOperation = weakOperation;
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
[self safelyRemoveOperationFromRunning:operation];
}
3.3>没有缓存,用户又不允许下载,则返回nil并讲operation移除。
else {
// Image not in cache and download disallowed by delegate
__strong __typeof(weakOperation) strongOperation = weakOperation;
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:nil data:nil error:nil cacheType:SDImageCacheTypeNone finished:YES url:url];
[self safelyRemoveOperationFromRunning:operation];
}
其他接口
取消所有任务,从正在执行的任务数组中获取所有任务对象,并调用它们的cancel方法
- (void)cancelAll {
@synchronized (self.runningOperations) {
NSArray<SDWebImageCombinedOperation *> *copiedOperations = [self.runningOperations copy];
[copiedOperations makeObjectsPerformSelector:@selector(cancel)];
[self.runningOperations removeObjectsInArray:copiedOperations];
}
}
//检测sd当前是否正在执行任务
- (BOOL)isRunning {
BOOL isRunning = NO;
@synchronized (self.runningOperations) {
isRunning = (self.runningOperations.count > 0);
}
return isRunning;
}
//安全移除任务,由于runningOperations可能被多个线程同时访问,所以需要进行加锁
- (void)safelyRemoveOperationFromRunning:(nullable SDWebImageCombinedOperation*)operation {
@synchronized (self.runningOperations) {
if (operation) {
[self.runningOperations removeObject:operation];
}
}
}
小结:
- 调度层下载的主要流程是什么
查找缓存,若缓存中没有 image 则通过 SDWebImageDownloader 来进行下载,下载完成后通过 SDImageCache 进行缓存,会同时缓存到 memCache 和 diskCache 中
- 为什么下载成功要将url从失败数组中移除。
因为[self.failedURLs addObject:url]是只在下载失败时添加的,而下载成功和下载失败是互斥的,也就是说,下载成功时failedURLs数组里就不应该有这个url,为什么要这么写呢,这是为了解决竞态条件下的问题,若两个线程下载同一个url的图片,若第一个线程下载失败,第二个下载成功。如果不从failedURLs移除这个url的话,以后下载此url的图片都会失败。
- 为什么要返回返回的SDWebImageCombinedOperation类型
这个类型包含NSOperation *cacheOperation的一个子类型,其中cacheOperation中又存在id <SDWebImageOperation>的下载图片的subOperation。在cancel的时候也应该把这两个操作都cancle。