AFNetworking implementation principle understanding

NSURLSession:

NSURLSession consists of three basic modules:

NSURLSession

NSURLSessionConfiguation

NSURLSessionTask

NSURLSession is relative to the session in normal communication, but it does not transmit network data itself. It will wear multiple NSURLSessionTask to execute each network request.

The behavior of NSURLSession depends on three things. Including the type of NSURLSession, the type of NSURLSessionTask and whether the APP is in the front end when the task is created

There are three types of NSURLSession

defaultSession stores cache and creditials locally

Ephemeral Session is more confidential and secure to data, and does not store any data locally. It stores cache and creditials in memory and binds it to the Session. When the Session is destroyed, the corresponding data will also be destroyed.

BackgroundSession can continue data transfer when the APP is in the background, its behavior is similar to defaultSession, but all data transfers are managed by a process other than the APP. There are also some functional limitations.

Configured through NSURLSessionConfigration when creating the Session object, you can set the delegate of the Session

Once the Session is configured, it cannot be modified unless a new Session object is created.

NSURLSessionTask includes three Task types, namely: NSURLSessionDataTask, NSURLSessionDownLoadTask, NSURLSessionUploadTask

All Task states are suspended, you need to start the Task with [Task resume]

NSURLSession has two ways to get data:

Specify the delegate when initializing the session and return data in the proxy method. You need to implement two proxy methods of NSURLSession

If the delegate is not specified when the Session is initialized, the data is returned through the block callback.

There are two destruction modes for the destruction of NSURLSession objects:

- (void)invalidateAndCancel Cancels all tasks in the Session, destroys all delegates, blocks, and Session itself. After the call, the Session cannot be reused.

- (void)finishTasksAndInvalidate will return immediately, but will not cancel the started tasks, but call delegate when these tasks are completed

There is one point to note here, that is: the NSURLSession object has a strong reference to its delegate, and only when the Session object is invalidate will the delegate be released, otherwise there will be a memory leak.

Use Session to speed up network access, use tasks in the same Session to access data, without having to implement three-way handshake every time, reuse the previous network link between the server and the client, thereby speeding up the access speed.

AFNetworking:

AFNetworking is a network request that encapsulates NSURLSession

AFNetworking consists of five modules:

It consists of five parts: NSURLSession, Security, Reachability, Serialization, and UIKit.

NSURLSession: Network communication module (core module) Corresponding to AFURLSessionManager in AFNetworking and AFHTTPSessionManager that specialises the HTTP protocol, AFHTTPSessionManager is inherited from AFURLSessionmanager

Security: Network communication security policy module corresponds to AFSecurityPolicy

Reachability: The network status monitoring module corresponds to AFNetworkReachabilityManager

Seriaalization: The serialization and deserialization module of network communication information corresponds to AFURLResponseSerialization

UIKit: Extension library for IOSUIKit

The process of network request:

Create NSURLSessionConfig object--initialize NSURLSession with the created config object configuration--create NSURLSessionTask object and resume execution, return data with delegate or block callback.

AFURLSessionManager encapsulates the above network interaction functions

AFURLSessionManager request process

1. Initialize AFURLSessionManager.

2. Get the Task object of AFURLSessionManager

3. Start Task

AFURLSessionManager will create an AFURLSessionmanagerTaskDelegate object for each Task, and the manager will let it handle the specific transactions of each Task, thus realizing the management of multiple Tasks by the manager

After initializing the manager, obtain a Task for a network request, generate a Task object, create an AFURLSessionmanagerTaskDelegate and associate it, set the upload and download delegates of the Task, and monitor the download progress and upload progress through KVO

Response from NSURLSessionDelegate

Because the delegate of AFURLSession managed by AFURLSessionmanager points to itself, all

The callback addresses of NSURLSessiondelegate are all AFURLSessionmanager, and AFURLSessionmanager will pass the delegate responded by the AF delegate to the corresponding AF delegate according to whether specific processing is required.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326363422&siteId=291194637