你要知道的NSURLSession都在这里
转载请注明出处 http://www.jianshu.com/p/6a9fb7c85251
本系列文章主要讲解iOS中网络请求类NSURLSession的使用方法进行详解,同时也会以此为扩展,讲解SDWebImage中图片下载功能的源码分析,讲解AFNetworking相关源码分析。本系列文章主要分为以下几篇进行讲解,读者可按需查阅。
- iOS网络——NSURLSession详解及SDWebImage源码解析
- iOS网络——SDWebImage SDImageDownloader源码解析
- iOS网络——AFNetworking AFURLSessionManager源码解析
- iOS网络——AFNetworking AFHttpSessionManager源码解析
SDWebImage SDWebImageDownloader源码解析
前一篇文章中讲解了SDWebImageDownloaderOperation
是如何自定义NSOperation
子类以及如何使用NSURLSession
实现下载的,本文将会讲解SDWebImageDownloader
类,来探索SDWebImage
如何实现多线程下载多张图片的。
首先看一下接口声明代码:
//下载选项设置的一系列枚举
typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
SDWebImageDownloaderLowPriority = 1 << 0,
SDWebImageDownloaderProgressiveDownload = 1 << 1,
SDWebImageDownloaderUseNSURLCache = 1 << 2,
SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
SDWebImageDownloaderContinueInBackground = 1 << 4,
SDWebImageDownloaderHandleCookies = 1 << 5,
SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
SDWebImageDownloaderHighPriority = 1 << 7,
SDWebImageDownloaderScaleDownLargeImages = 1 << 8,
};
//下载图片时的顺序,FIFO或者LIFO
typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
SDWebImageDownloaderFIFOExecutionOrder,
SDWebImageDownloaderLIFOExecutionOrder
};
//声明通知的全局变量名
FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStartNotification;
FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStopNotification;
//进度回调块
typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL);
//下载完成的回调块
typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
//http首部数据字典
typedef NSDictionary<NSString *, NSString *> SDHTTPHeadersDictionary;
//http首部可变数据字典
typedef NSMutableDictionary<NSString *, NSString *> SDHTTPHeadersMutableDictionary;
//http首部过滤的一个回调块
typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterBlock)(NSURL * _Nullable url, SDHTTPHeadersDictionary * _Nullable headers);
上面就是一些枚举、变量、自定义类型的声明。
/*
自定义token类,用于取消下载任务
这个token第二个属性其实就是SDWebImageDownloaderOperation中使用的token即回调块的字典
目的相同,都是为了取消特定的下载任务
*/
@interface SDWebImageDownloadToken : NSObject
@property (nonatomic, strong, nullable) NSURL *url;
@property (nonatomic, strong, nullable) id downloadOperationCancelToken;
@end
//异步下载图片
@interface SDWebImageDownloader : NSObject
//是否压缩图片
@property (assign, nonatomic) BOOL shouldDecompressImages;
//支持的最大同时下载图片的数量,其实就是NSOperationQueue支持的最大并发数
@property (assign, nonatomic) NSInteger maxConcurrentDownloads;
//当前正在下载图片的数量,其实就是NSOperationQueue的operationCount即正在执行下载任务的operation的数量
@property (readonly, nonatomic) NSUInteger currentDownloadCount;
//下载时连接服务器的超时时间,默认15s
@property (assign, nonatomic) NSTimeInterval downloadTimeout;
//session运行模式,默认使用默认模式,即 defaultSessionConfiguration
@property (readonly, nonatomic, nonnull) NSURLSessionConfiguration *sessionConfiguration;
//执行下载任务的顺序,FIFO或LIFO
@property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder;
//类方法,获取全局共享的单例对象
+ (nonnull instancetype)sharedDownloader;
//默认的URL credential
@property (strong, nonatomic, nullable) NSURLCredential *urlCredential;
//用户名,有些图片下载的地址需要做用户认证
@property (strong, nonatomic, nullable) NSString *username;
//密码
@property (strong, nonatomic, nullable) NSString *password;
//过滤http首部的回调块
@property (nonatomic, copy, nullable) SDWebImageDownloaderHeadersFilterBlock headersFilter;
//初始化方法,不使用全局共享的downloader时创建
- (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)sessionConfiguration NS_DESIGNATED_INITIALIZER;
//为http首部设置值
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field;
//返回http首部的值
- (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field;
//设置下载类的Class,默认使用SDWebImageDownloaderOperation,开发者可以自定义只需实现相关协议
- (void)setOperationClass:(nullable Class)operationClass;
/*
下载url对应的图片
设置下载配置选项、进度回调块、下载完成回调块
返回一个token,用于取消对应的下载任务
*/
- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
options:(SDWebImageDownloaderOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
//取消下载任务,需要传入上面创建下载任务时返回的token
- (void)cancel:(nullable SDWebImageDownloadToken *)token;
//设置下载队列NSOperationQueue挂起
- (void)setSuspended:(BOOL)suspended;
//设置取消NSOperationQueue队列中的所有下载任务
- (void)cancelAllDownloads;
//要求downloader使用特定运行模式创建一个NSURLSession对象
- (void)createNewSessionWithConfiguration:(nonnull NSURLSessionConfiguration *)sessionConfiguration;
@end
上面定义了一系列的属性和方法,接下来看一下.m文件
的源码:
@implementation SDWebImageDownloadToken
@end
//遵守NSURLSessionTaskDelegate和NSURLSessionDataDelegate协议
@interface SDWebImageDownloader () <NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
//定义一个NSOperationQueue的下载队列
@property (strong, nonatomic, nonnull) NSOperationQueue *downloadQueue;
//最近一次添加进队列的operation主要用于LIFO时设置依赖
@property (weak, nonatomic, nullable) NSOperation *lastAddedOperation;
//operationClass默认是SDWebImageDownloaderOperation
@property (assign, nonatomic, nullable) Class operationClass;
//可变字典,key是图片的URL,value是对应的下载任务Operation
@property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, SDWebImageDownloaderOperation *> *URLOperations;
//<NSString*, NSString*>类型的字典,存储http首部
@property (strong, nonatomic, nullable) SDHTTPHeadersMutableDictionary *HTTPHeaders;
//一个GCD的队列
@property (SDDispatchQueueSetterSementics, nonatomic, nullable) dispatch_queue_t barrierQueue;
//NSURLSession对象
@property (strong, nonatomic) NSURLSession *session;
@end
@implementation SDWebImageDownloader
//类方法,类加载的时候执行
+ (void)initialize {
//如果导入了SDNetworkActivityIndicator文件,就会展示一个小菊花
if (NSClassFromString(@"SDNetworkActivityIndicator")) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
id activityIndicator = [NSClassFromString(@"SDNetworkActivityIndicator") performSelector:NSSelectorFromString(@"sharedActivityIndicator")];
#pragma clang diagnostic pop
//删除通知后重新添加通知,防止重复添加出现异常
[[NSNotificationCenter defaultCenter] removeObserver:activityIndicator name:SDWebImageDownloadStartNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:activityIndicator name:SDWebImageDownloadStopNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:activityIndicator
selector:NSSelectorFromString(@"startActivity")
name:SDWebImageDownloadStartNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:activityIndicator
selector:NSSelectorFromString(@"stopActivity")
name:SDWebImageDownloadStopNotification object:nil];
}
}
//类方法,返回单例对象
+ (nonnull instancetype)sharedDownloader {
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}
//初始化方法调用下面的初始化方法,NSURLSession运行在默认模式下
- (nonnull instancetype)init {
return [self initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}
//初始化方法
- (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)sessionConfiguration {
if ((self = [super init])) {
//默认使用SDWebImageDownloaderOperation作为下载任务Operation
_operationClass = [SDWebImageDownloaderOperation class];
_shouldDecompressImages = YES;
//默认下载顺序FIFO
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
//创建NSOperationQueue并设置最大并发数为6,即同时最多可以下载6张图片
_downloadQueue = [NSOperationQueue new];
_downloadQueue.maxConcurrentOperationCount = 6;
//设置名称
_downloadQueue.name = @"com.hackemist.SDWebImageDownloader";
//设置下载webp格式图片的http首部
_URLOperations = [NSMutableDictionary new];
#ifdef SD_WEBP
_HTTPHeaders = [@{@"Accept": @"image/webp,image/*;q=0.8"} mutableCopy];
#else
_HTTPHeaders = [@{@"Accept": @"image/*;q=0.8"} mutableCopy];
#endif
//创建一个GCD并发队列
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
//默认超时时间15s
_downloadTimeout = 15.0;
//创建一个sessionConfiguration运行默认的NSURLSession对象
[self createNewSessionWithConfiguration:sessionConfiguration];
}
return self;
}
//创建指定运行模式的NSURLSession对象,可能已经创建过了
- (void)createNewSessionWithConfiguration:(NSURLSessionConfiguration *)sessionConfiguration {
//可能已经创建过,所以需要取消前一个session的下载任务并打破引用循环
[self cancelAllDownloads];
if (self.session) {
[self.session invalidateAndCancel];
}
sessionConfiguration.timeoutIntervalForRequest = self.downloadTimeout;
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
}
//在析构函数中调用session的invalidateAndCancel方法取消下载任务并打破引用循环
- (void)dealloc {
[self.session invalidateAndCancel];
self.session = nil;
//NSOperationQueue取消所有的下载操作
[self.downloadQueue cancelAllOperations];
//释放GCD队列
SDDispatchQueueRelease(_barrierQueue);
}
上面的各种初始化、析构函数也都很简单,不再赘述了。
//为http首部设置键值对
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field {
if (value) {
self.HTTPHeaders[field] = value;
} else {
[self.HTTPHeaders removeObjectForKey:field];
}
}
//获取http首部的值
- (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field {
return self.HTTPHeaders[field];
}
//设置最大同时下载图片的数量,即NSOperationQueue最大并发数
- (void)setMaxConcurrentDownloads:(NSInteger)maxConcurrentDownloads {
_downloadQueue.maxConcurrentOperationCount = maxConcurrentDownloads;
}
//当前正在下载图片数量,即NSOperationQueue中正在执行的operation数量
- (NSUInteger)currentDownloadCount {
return _downloadQueue.operationCount;
}
//获取最大同时下载图片的数量
- (NSInteger)maxConcurrentDownloads {
return _downloadQueue.maxConcurrentOperationCount;
}
//获取NSURLSession的运行模式配置
- (NSURLSessionConfiguration *)sessionConfiguration {
return self.session.configuration;
}
//设置operation的Class类对象
- (void)setOperationClass:(nullable Class)operationClass {
if (operationClass && [operationClass isSubclassOfClass:[NSOperation class]] && [operationClass conformsToProtocol:@protocol(SDWebImageDownloaderOperationInterface)]) {
_operationClass = operationClass;
} else {
_operationClass = [SDWebImageDownloaderOperation class];
}
}
上面的方法都是一些setter
和getter
。
//下载图片的方法
- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
options:(SDWebImageDownloaderOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock {
__weak SDWebImageDownloader *wself = self;
//直接调用另一个方法,后面大片的block代码目的就是为了创建一个SDWebImageDownloaderOperation类的对象
return [self addProgressCallback:progressBlock completedBlock:completedBlock forURL:url createCallback:^SDWebImageDownloaderOperation *{
//block中为了防止引用循环和空指针,先weak后strong
__strong __typeof (wself) sself = wself;
//设置超时时间
NSTimeInterval timeoutInterval = sself.downloadTimeout;
if (timeoutInterval == 0.0) {
timeoutInterval = 15.0;
}
//设置缓存策略
NSURLRequestCachePolicy cachePolicy = options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData;
//创建一个可变的request镀锡
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url
cachePolicy:cachePolicy
timeoutInterval:timeoutInterval];
//设置cookie的处理策略
request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);
request.HTTPShouldUsePipelining = YES;
//过滤http首部
if (sself.headersFilter) {
request.allHTTPHeaderFields = sself.headersFilter(url, [sself.HTTPHeaders copy]);
}
else {
request.allHTTPHeaderFields = sself.HTTPHeaders;
}
//创建一个SDWebImageDownloaderOperation类的对象
//传入request、session和下载选项配置options
SDWebImageDownloaderOperation *operation = [[sself.operationClass alloc] initWithRequest:request inSession:sself.session options:options];
//设置是否压缩图片
operation.shouldDecompressImages = sself.shouldDecompressImages;
//设置认证凭证和https相关
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;
}
//向队列中添加创建的下载任务,之后这个operation就会被线程调度来执行其start方法
[sself.downloadQueue addOperation:operation];
//如果是LIFO就设置一个依赖
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;
}
//返回operation
return operation;
}];
}
//取消一个下载任务,需要传入上一个方法返回的token,其实具体的token是由下一个方法创建的
- (void)cancel:(nullable SDWebImageDownloadToken *)token {
//异步执行,阻塞队列从而串行的删除任务,可以避免竞争条件的产生
dispatch_barrier_async(self.barrierQueue, ^{
//通过token的url获取到这个Operation
SDWebImageDownloaderOperation *operation = self.URLOperations[token.url];
//调用Operation自定义的cancel方法来取消任务,传入一个回调块字典的token
BOOL canceled = [operation cancel:token.downloadOperationCancelToken];
//如果取消了就从字典中移除掉这个键值对
if (canceled) {
[self.URLOperations removeObjectForKey:token.url];
}
});
}
/*
前面download方法调用的方法,返回一个token
createCallback就是download方法写的,用于创建一个SDWebImageDownloaderOperation
*/
- (nullable SDWebImageDownloadToken *)addProgressCallback:(SDWebImageDownloaderProgressBlock)progressBlock
completedBlock:(SDWebImageDownloaderCompletedBlock)completedBlock
forURL:(nullable NSURL *)url
createCallback:(SDWebImageDownloaderOperation *(^)(void))createCallback {
//URL为nil就调用下载完成回调块,返回nil
if (url == nil) {
if (completedBlock != nil) {
completedBlock(nil, nil, nil, NO);
}
return nil;
}
//定义一个token变量
__block SDWebImageDownloadToken *token = nil;
//同步方法,阻塞当前线程也阻塞队列,防止产生竞争条件
dispatch_barrier_sync(self.barrierQueue, ^{
//通过URL获取Operation
SDWebImageDownloaderOperation *operation = self.URLOperations[url];
if (!operation) {
//如果URL对应的Operation不存在就调用,createCallback块创建一个
operation = createCallback();
//添加进字典中
self.URLOperations[url] = operation;
__weak SDWebImageDownloaderOperation *woperation = operation;
//设置Operation下载完成的回调块
operation.completionBlock = ^{
//同步方法阻塞当前线程阻塞队列
dispatch_barrier_sync(self.barrierQueue, ^{
//先weak后strong
SDWebImageDownloaderOperation *soperation = woperation;
if (!soperation) return;
//下载完成就从字典中删除
if (self.URLOperations[url] == soperation) {
[self.URLOperations removeObjectForKey:url];
};
});
};
}
//取消下载任务时的token,第一个值就是url,第二个值就是回调块字典
id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock];
token = [SDWebImageDownloadToken new];
token.url = url;
token.downloadOperationCancelToken = downloadOperationCancelToken;
});
return token;
}
上面的方法也比较容易懂,不再赘述了。
//设置是否挂起下载队列
- (void)setSuspended:(BOOL)suspended {
self.downloadQueue.suspended = suspended;
}
//取消所有的下载任务
- (void)cancelAllDownloads {
[self.downloadQueue cancelAllOperations];
}
#pragma mark Helper methods
//通过NSURLSessionTask找到NSOPerationQueue里的任务
- (SDWebImageDownloaderOperation *)operationWithTask:(NSURLSessionTask *)task {
SDWebImageDownloaderOperation *returnOperation = nil;
for (SDWebImageDownloaderOperation *operation in self.downloadQueue.operations) {
if (operation.dataTask.taskIdentifier == task.taskIdentifier) {
returnOperation = operation;
break;
}
}
return returnOperation;
}
#pragma mark NSURLSessionDataDelegate
//下面这些代理方法,都是先通过NSURLSessionTask找到对应的SDWebImageDownloaderOperation
//然后调用Operation的代理方法,
- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
// Identify the operation that runs this task and pass it the delegate method
SDWebImageDownloaderOperation *dataOperation = [self operationWithTask:dataTask];
[dataOperation URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
// Identify the operation that runs this task and pass it the delegate method
SDWebImageDownloaderOperation *dataOperation = [self operationWithTask:dataTask];
[dataOperation URLSession:session dataTask:dataTask didReceiveData:data];
}
- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
willCacheResponse:(NSCachedURLResponse *)proposedResponse
completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler {
// Identify the operation that runs this task and pass it the delegate method
SDWebImageDownloaderOperation *dataOperation = [self operationWithTask:dataTask];
[dataOperation URLSession:session dataTask:dataTask willCacheResponse:proposedResponse completionHandler:completionHandler];
}
#pragma mark NSURLSessionTaskDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
// Identify the operation that runs this task and pass it the delegate method
SDWebImageDownloaderOperation *dataOperation = [self operationWithTask:task];
[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 {
completionHandler(request);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
// Identify the operation that runs this task and pass it the delegate method
SDWebImageDownloaderOperation *dataOperation = [self operationWithTask:task];
[dataOperation URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler];
}
@end
这里的代理方法运用比较值得学习吧,SDWebImageDownloaderOperation
需要传入一个NSURLSession
对象,但是这个对象不一定可用,如果不可用SDWebImageDownloaderOperation
就会自己创建一个NSURLSession
对象,但如果它可用,那SDWebImageDownloaderOperation
就不能接收回调方法,所以,本类在回调方法中直接调用SDWebImageDownloaderOperation
的回调方法。
经过两篇文章的源码讲解,SDWebImage
关于图片下载的部分也就全部讲解完了,主要使用了自定义NSOperation
子类,并在这个自定义NSOperation
子类中通过一个可用的NSURLSession
来创建一个执行服务器交互数据的NSURLSessionDataTask
的下载任务,并由其全权负责下载工作,接着使用NSOperationQueue
实现多线程的多图片下载。源码中值得我们学习的地方有很多,比如,在设计第三方库时要设计全面的通知,为了防止竞争条件可以使用一个串行队列或是barrier方法来执行一些可能会产生多线程异常的代码,还有很多设计代码的细节需要我们自行体会。
备注
由于作者水平有限,难免出现纰漏,如有问题还请不吝赐教。
网友评论