这是一篇AFNetworking的详细解析
使用示例
新建一个AFHTTPSessionManager的manager,然后用manager请求对应网址的信息,回调会调用success的completionHander代码块。
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
[manager GET:@"www.baidu.com"
parameters:nil
progress:nil
success:^(NSURLSessionDataTask * _Nonnull task,
id _Nullable responseObject) {
NSLog(responseObject);
} failure:nil];
AFNetworking流程图
- 调用post请求
- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters
progress:(void (^)(NSProgress * _Nonnull))uploadProgress
success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
failure:(void (^)(NSURLSessionDataTask * _Nullable,
NSError * _Nonnull))failure
- 新建一个task
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress))
downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
- 新建一个request
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
error:(NSError *__autoreleasing *)error
- 新建一个dataTask
- (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
- 添加delegate
- (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
整体架构
- AFURLSessionManager的mutableTaskDelegatesKeyedByTaskIdentifier属性,存放着每一个task对应的AFURLSessionManagerTaskDelegate;
- AFURLSessionManager的NSURLSessionDelegate实现,会调用每个task对应的AFURLSessionManagerTaskDelegate;
- 设置Task的AFURLSessionManagerTaskDelegate时,用了NSLock,进行多线程同步;
- 任务的调度采用了工厂模式、观察者和代理模式。
-
工厂模式
对于HTTP协议的请求有
AFHTTPRequestSerializer
AFJSONRequestSerializer
AFPropertyListRequestSerializer
三个都依赖抽象接口AFURLRequestSerialization
对于HTTP协议的响应有
AFHTTPResponseSerializer
AFJSONResponseSerializer
AFXMLParserResponseSerializer
AFXMLDocumentResponseSerializer
AFPropertyListResponseSerializer
AFImageResponseSerializer
AFCompoundResponseSerializer
都依赖抽象接口 AFURLResponseSerialization
符合 开放-封闭原则
对增加新的请求头、响应头是开放的,而且增加新内容不需要修改原内容 -
观察者模式
对于task的每个生命周期,都会有相应的事件发出
AFNetworkingTaskDidResumeNotification
AFNetworkingTaskDidSuspendNotification
对于task用KVO的方式去监听Progress -
代理模式
AFURLSessionManagerTaskDelegate 是AF对自己的逻辑封装,同时实现了
NSURLSessionTaskDelegate,
NSURLSessionDataDelegate
NSURLSessionDownloadDelegate
同时通过实现NSURLSessionDelegate,来接受session级别的事件。
-
工厂模式