NSURLSession is a new generation of network framework launched by iOS7. On the basis of NSURLConnection, AFNetwrking also implements the corresponding network request API based on NSURLSession. From the functional module division, network availability, data serialization and deserialization and security policy these parts are the same, mainly network request management, request and return processing is simpler than the former, it should be said that NSURLSession from the system level to achieve the part that NSURLConnection does not achieve, For example, AFNetworking manages multiple requests and provides an interface for uploading and downloading directly to the application layer. Therefore, AFNetworking implements a simpler network layer based on NSRULSession, but it is not as rich as NSURLConnection in terms of functions, such as monitoring the progress of all tasks and integrating with UIKit.
AFURLSessionManager
- As the core of the network layer, NSURLSession supports the management of multiple task network requests. Have NSURLSessionDelegate NSURLSessionTas kDelegate NSURLSessionDataDelegate, NSURLSessionDownloadDelegate multiple proxy interface.
* operationQueue is used to initialize the queue of NSURLSession in which the callback from the network request is executed. There is also an OperationQueue, similar to NSURLConnection. Although NSURLConnection itself is an asynchronous thread that initiates network requests, if NSOpeartion and OperationQueue are not used, Network callbacks will be executed in the main thread, affecting smooth UI operations. In the implementation part of NSURLConnection, callbacks are performed through the global network thread. This is where NSURLConnection differs from NSURLSession, which is handled by the system layer and is much more convenient than NSURLConnection.
-
ResponseSerializer is the deserialized object used to return data
-
All network requests managed by the Tasks NSURLSession consist of repetitive, generic API requests, upload requests, and download requests.
* dataTasks general API requests
* uploadTasks upload request
DownloadTasks download requests
* completionQueue Specifies the queue in which the user’s callback is located
* completionGroup Specifies the group to which the client callback queue belongs
* AFURLSessionManagerTaskDelegagate within AFURLSessionManager used to store the task related data of business object, details below will introduce alone.
* mutableTaskDelegatesKeyedByTaskIdentifier mutable dictionary, used to store the link between the task and the corresponding to the delegate.
All that is left is the callback block object for various states during the network request.
AFURLSessionManagerTaskDelegate
It is used to store and record information related to a single task during various callbacks to network requests, and to return the data to the client after the network request completes.
- AFURLSessionManager in AFURLSessionManagerTaskDelegate object record the session object, a session object corresponding to the multiple task object, Each task has a AFURLSessionManagerTaskDelegate counterpart of object. Each task shares some data at the session level, such as serialized objects, callback queues and groups. When a task completes, it needs to obtain the data from the shared session to complete the callback logic at the business layer.
* mutableData stores the returned data information obtained by the current request, which is eventually deserialized into an object or stored as a file.
-
Progress records the progress of downloading or uploading files. You can obtain the progress of a task by using uploadProgressForTask at any time.
-
AFURLSessionTaskCompletionHandler set by client, the client callback block after completion of the task request
* AFURLSessionDownloadTaskDidFinishDownloadingBlock set by user, after completion of task to download file generated block of the preservation of the path to the target file
DownloadFileURL The path to the object file generated by the previous block, set during the download completion callback, and then returned to the client as a notification during the entire request completion callback
AFURLSessionManager AFURLSessionManagerTaskDelegate relationships
In AFURLSessionManager most NSURLSession agreement is achieved, and AFURLSessionManagerTaskDelegate also achieved several important agreements, such as data acquisition, the task request is completed, the progress of the file download and upload. The difference is that AFURLSessionManager is for all requests, while taskDelegate is for a specific request. Instead of getting callbacks directly from the system layer, all callbacks are handled by AFURLSessionManager and then distributed to each specific taskDelegate, who stores and records data and information related to the current request. As for AFURLSessionManagerTaskDelegate would be similar to the following correction in the implementation of the logic:
- (void)URLSession:(__unused NSURLSession *)session
task:(__unused NSURLSessionTask *)task
didSendBodyData:(__unused int64_t)bytesSent
totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
self.progress.totalUnitCount = totalBytesExpectedToSend;
self.progress.completedUnitCount = totalBytesSent;
}
Copy the code
The task delegate here only retrieves useful data and keeps the upload progress, which is intended to keep the code consistent and easier to read, and is equivalent to the delegate method distribution. SessionManager stores all tasks through dictionaries and handles the logic associated with each task in specific session callbacks.
AFHTTPSessionManager
It inherits from AFURLSessionManager, adding some initialization methods and error checking for serialization and deserialization phases, as well as setting specific serialized objects. AFURLSessionManager is just a more user-friendly interface wrapper.
NSURLSession and NSURLConnection
AFNetworking NSRULSession in the realization of the design of basic is consistent, AFURLSessionManager AFHTTPRequestOperationManager. AFURLConnectionOperation is similar to NSURLSessionTask. However, the implementation of network callback is inconsistent, the former is handled by NSURLSession, and then distributed to each task to handle itself. In NSURLConnection, each request handles its own network callback from the system layer independently, due to the different system network framework. One problem with both implementations is that if the baseURL is constantly changing, when downloading a material, it is better to use only AFURLConnectionPeration in NSRULConnection, and specify the URL each time. To maintain OperationQueue, may need to be part of the implementation AFHTTPRequestOperationManager.