Understanding the use of af3.0
af3.0 no longer uses "AFHTTP Request Operation" and only uses NSURLSession, so let's start with how to use NSURLSession to initiate requests
- What network requests to start under config
- NSURLSession uses config to establish connections
- What type of request data is NSURLSession Task, up, down
-
Session & task's delegate
af3.0 includes the following modules
- Network Communication Module (NSURLSession)
- Reachability
- Network Communication Security Policy Module
- Serialization/Deserialization Module of Network Communication Information
- Extensions to the iOS UIKit Library (UIKit)
For the next few modules, do not do analysis for the time being. First, focus on AFURLSession Manager and AFHTTPSession Manager to do an analysis.
1. Understanding AFURLSession Manager for Core Modules
AFURLSession Manager encapsulates three modules: Reachability, Security and Serialization to ensure the completion of network requests, uses task agents to obtain data, and finally provides the corresponding interfaces of data, up and down.
//AFURLSession Manager manages NSOperationQueue, ResponseSerializer, Security Policy, Reachability Manager, tasks, completionQueue; @property (readonly, nonatomic, strong) NSURLSession *session; /* .. Other member variables .. */ //The open interface of AFURLSession Manager, data,up,down - (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; /* .. Other Open Interfaces .. */
2. Understanding AFHTTPSession Manager
AFHTTPSession Manager is a further encapsulation of AFURLSession Manager, with open interfaces such as baseUrl, requestSerializer, responseSerializer and get/post.
//Interface NSURL *baseURL; AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer; AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer; //Other requests get,head,post,put,patch,delete - (nullable NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(nullable id)parameters progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; /*..Other interfaces..*/
3. Initiate requests using AFHTTPSession Manager
Create session manager and then make a request
AFHTTPSessionManager *afManager = [AFHTTPSessionManager manager]; afManager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:urlStr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //data processing } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
4. Initiate requests using AFURLSession Manager
With AFHTTPSession Manager, you can't customize some complex requests, so you can use AFURLSession Manager. Specifically, there are the following customizable places
- SURLSession can set up access to cellular non-access under wifi, etc.
- A NSURLSession can set up multiple task queues
// Step 1. Initialize the AFURLSessionManager object NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; // Step 2. Get the task object of AFURLSession Manager NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { NSLog(@"Error: %@", error); } else { NSLog(@"Get Net data success!"); } }]; // Step 3. Launch task [dataTask resume];
Some changes in af3.0 vs. 2
- af inherits the advantages of urlSession and then encapsulates proxy; encapsulates UI-based classification extension; adds network monitoring and network security modules; encapsulates the serialization of requests and returns. It is more convenient to use.
- Af's manage ment method does not use singletons. In fact, when looking up the source code, we found that the af2.0 era has not used singletons. Personal understanding is to facilitate the customization of network requests (if you have questions, please answer for me).
- In the age of 2.0, operation Queue was used to establish a sub-thread request network. After 3.0, an operation Queue was bound by system urlSession to realize asynchronous UI request initiation.
-
When used, 2-3 single sessions can be established to customize network requests, and other network requests can customize access based on different single sessions.
Some references
- NSURLSession versus NSURLConnection & Using Shared Session Manager/Session
- Use of NSURLSession in AFNetWorking(3.0)
- AFNetworking 3.0 Source Code Interpretation Series (Parsing)
-
Learning summary after reading af
Limited capacity. Welcome to the discussion.