3个全局队列
创建task时用af_url_session_manager_creation_queue,有3处用处,data task, upload task(文件,data, stream), download task(request, resume data)
处理请求/下载数据时用af_url_session_manager_processing_queue,比如解析response data
请求返回时用af_url_session_manager_completion_group,但是每个AFURLSessionManager有属性completionGroup, 优先使用属性completionGroup,如无则使用全局队列
NSLock的使用
添加/删除 task的notification, 修改mutableTaskDelegatesKeyedByTaskIdentifier。
NSLock lock;
....;
NSLock unlock
task notification的转发
AFNSURLSessionTaskDidResumeNotification, AFNSURLSessionTaskDidSuspendNotification 作为类的内部使用,AFNetworkingTaskDidResumeNotification,AFNetworkingTaskDidSuspendNotification 作为外部分发使用。
Delegate
3个系统回调:NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate
AFURLSessionManger实现回调,在初始化NSURLSession时:NSURLSession* session = [session sessionWithConfigruation: delegate:delegateQueue:]
AFURLSessionManager转发回调给各个AFURLSessionManagerTaskDelegate, 有URLSession:task:didCompleteWithError:, URLSession:task:didFinishCollectingMetrics:, URLSession:dataTask:didReceiveData:, URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:, URLSession:downloadTask:didResumeAtOffset:expectedTototalBytes;
信号量
查询4个task数组时候, tasks, dataTasks, uploadTasks, downloadTasks
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSSession getTask....{
dispatch_semaphore_signal(sema);
}
dispatch_semaphore_wait(sema)
task
NSURLSessionDataTask, NSURLSessionUploadTask, NSURLSessionDownloadTask
NSURLSession dataTaskWithRequest:, uploadTaskWithRequest, 为每个task创建一个AFURLSessionManagerTaskDelegate, 存储于mutableTaskDelegateKeyedByTaskIdentifier种,以task.identifier为key. 监听每个task的resume和suspend消息。
网友评论