最近看了下AFNetworking3.0的代码,发现大师写的代码有很多亮点,其中我觉得最可以借鉴的是模块的解耦性。
解耦性
应用方使用AF一般只会用到AFHTTPSessionManger类,而这个类只不过是封装了AFURLSessionManger类的方法,所以主要功能的实现都在AFURLSessionManger类。
AFURLSessionManger类里面是如何解耦的呢?其中最主要的是,定义了一个字典
@property (readwrite, nonatomic, strong) NSMutableDictionary *mutableTaskDelegatesKeyedByTaskIdentifier;
该mutableTaskDelegatesKeyedByTaskIdentifier的key值为NSURLSessionTask.taskIdentifier(一个NSURLSessionTask任务的id标识),value值为AFURLSessionManagerTaskDelegate。
而AFURLSessionManager需要实现的NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate代理,主要都是通过mutableTaskDelegatesKeyedByTaskIdentifier字典抛回给AFURLSessionManagerTaskDelegate里去实现,实现完成的值又通过block回调给AFURLSessionManager里面的block。
具体流程:
1、 AFURLSessionManager新建一个Task时,把Task与AFURLSessionManagerTaskDelegate通过字典mutableTaskDelegatesKeyedByTaskIdentifier联系起来
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler {
__block NSURLSessionDataTask *dataTask = nil;
url_session_manager_create_task_safely(^{
dataTask = [self.session dataTaskWithRequest:request];
});
[self addDelegateForDataTask:dataTask uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler];
return dataTask;
}
其中关键代码:
[self addDelegateForDataTask:dataTask uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler];
- (void)addDelegateForDataTask:(NSURLSessionDataTask *)dataTask
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] initWithTask:dataTask];
delegate.manager = self;
delegate.completionHandler = completionHandler;
dataTask.taskDescription = self.taskDescriptionForSessionTasks;
[self setDelegate:delegate forTask:dataTask];
delegate.uploadProgressBlock = uploadProgressBlock;
delegate.downloadProgressBlock = downloadProgressBlock;
}
2、在回调函数中,把需要实现的委托,抛回给AFURLSessionManagerTaskDelegate去实现,抛回后从字典中移除该Task
- (void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:task];
// delegate may be nil when completing a task in the background
if (delegate) {
[delegate URLSession:session task:task didCompleteWithError:error];
[self removeDelegateForTask:task];
}
if (self.taskDidComplete) {
self.taskDidComplete(session, task, error);
}
}
其中关键代码为:
[delegate URLSession:session task:task didCompleteWithError:error];
[self removeDelegateForTask:task];
3、AFURLSessionManagerTaskDelegate去实现该委托,实现完成后把结果给self.completionHandler 的block值,从而就把该值传递给了应用层的completionHandler
- (void)URLSession:(__unused NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
__strong AFURLSessionManager *manager = self.manager;
__block id responseObject = nil;
__block NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[AFNetworkingTaskDidCompleteResponseSerializerKey] = manager.responseSerializer;
//Performance Improvement from #2672
NSData *data = nil;
if (self.mutableData) {
data = [self.mutableData copy];
//We no longer need the reference, so nil it out to gain back some memory.
self.mutableData = nil;
}
if (self.downloadFileURL) {
userInfo[AFNetworkingTaskDidCompleteAssetPathKey] = self.downloadFileURL;
} else if (data) {
userInfo[AFNetworkingTaskDidCompleteResponseDataKey] = data;
}
if (error) {
userInfo[AFNetworkingTaskDidCompleteErrorKey] = error;
dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
if (self.completionHandler) {
self.completionHandler(task.response, responseObject, error);
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
});
});
} else {
dispatch_async(url_session_manager_processing_queue(), ^{
NSError *serializationError = nil;
responseObject = [manager.responseSerializer responseObjectForResponse:task.response data:data error:&serializationError];
if (self.downloadFileURL) {
responseObject = self.downloadFileURL;
}
if (responseObject) {
userInfo[AFNetworkingTaskDidCompleteSerializedResponseKey] = responseObject;
}
if (serializationError) {
userInfo[AFNetworkingTaskDidCompleteErrorKey] = serializationError;
}
dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
if (self.completionHandler) {
self.completionHandler(task.response, responseObject, serializationError);
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
});
});
});
}
}
其中关键代码为
if (self.completionHandler) {
self.completionHandler(task.response, responseObject, error);
}
总结
其实就是把需要实现的Delegate值交给一个类去实现,好处是避免了随着业务不断发展出现回调函数里代码过长,而且直观性比较强,可以在我们工作中借鉴这种写法。最后,如果想看关于AF较为详细的源码分析,可以推荐看看这篇文章:
《AFNetworking到底做了什么》