Starting from 3.0
In a blink of an eye, AF has been updated to version 3.0. Currently the latest version on Cocoapods is 3.0 beta1. In the 3.0 version, AF is completely used NSURLSession
instead NSURLConnection
. It took some time to learn NSURLSession
, and learning here finally came in handy. Here is the main study of the use of version 3.0. Hopefully, the project will be able to go over to afnetwoking version 3.0 smoothly. In addition, with objective-c slowly being replaced by Swift, afnetworking 3.0 is probably the last big version of the update. This article will continue to update with an update, but also a continuous learning process.
Structure
In the 3.0 era, AFN streamlined the structure and was fully used NSURLSession
.
There are only a few managers left in Beta1.
AFHTTPSessionManager
is AFURLSessionManager
the child class.
Afurlsessionmanager
AFURLSessionManager
The following NSURLSession
proxy methods are implemented
NSURLSessionDelegate
URLSession:didBecomeInvalidWithError:
URLSession:didReceiveChallenge:completionHandler:
URLSessionDidFinishEventsForBackgroundURLSession:
NSURLSessionTaskDelegate
URLSession:willPerformHTTPRedirection:newRequest:completionHandler:
URLSession:task:didReceiveChallenge:completionHandler:
URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:
URLSession:task:didCompleteWithError:
NSURLSessionDataDelegate
URLSession:dataTask:didReceiveResponse:completionHandler:
URLSession:dataTask:didBecomeDownloadTask:
URLSession:dataTask:didReceiveData:
URLSession:dataTask:willCacheResponse:completionHandler:
NSURLSessionDownloadDelegate
URLSession:downloadTask:didFinishDownloadingToURL:
URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesWritten:totalBytesExpectedToWrite:
URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:
Member properties
In AFURLSessionManager
, the main three properties are as follows
session
A session is implemented and operationQueue
is an operation queue. responseSerializer
is an object that implements the AFURLResponseSerialization
protocol.
The manager also includes objects for security protocols and connectivity objects. These two classes will be discussed later.
The following is the contents of the task, including the current Session
Callback block queue, including two sections in the primary and private queues
Method
Initialize method
Create a NSURLSessionDataTask
data-sex task
Create an `NSURLSessionUploadTask
upload task
Create a NSURLSessionDownloadTask
download task
To get a specific taskprogress进度
Session Delegate Callbacks 设置会话代理回调
Task Delegate Callbacks 设置任务代理回调
When the task requires a new request body to send to the server.
This block is set when the HTTP request callback has a redirect
Set this challenge when a request requires special authentication
Set up a block to track upload progress
Set a block to execute when the task is completed
Setting Data Task Delegate Callbacks 设置数据任务代理的回调
Set a callback block when the data task gets response
Set a block to execute when the data task becomes a download task
Set up a block when the data task gets to the data
Set a block to absolutely cache data tasks
Download Task Delegate Callbacks 下载任务代理回调
Set the block to download after the task is completed
Set up block to track the progress of the download task
Set block to execute when loading task execution/recovery
The content of the header file is basically the above. You can see the whole AF system is very clear and complete, there is no superfluous things, the head file only exposes what should be exposed, it is worth learning.
Use the example used
AFURLSessionManager
From the source code can be seen, the AFURLSessionManager
realization of
1 |
NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSSecureCoding, NSCopying
|
First you need to set the URL and nsurlconfirguration
This is the public API using the Baidu API store
Then initialize the manager
Set Responseserializer
Initialize Request
Setting the request
Generate the corresponding according to request NSURLSessionTask
.
Perform tasks
Take a look at the post-execution information.
This is because of the use of the AFHTTPResponseSerializer
(API problem, only support Text/plain) so after getting the data, we ourselves want JSON serialization. If it is a well-designed API, it AFJSONRequestSerializer
can be used directly in the callback to get the data in JSON format.
As you can see, the whole use is still very convenient. We can configure different settings according to our own needs.
Afnetworking Study Notes