af3.0 Learning Use and Understanding

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

  1. What network requests to start under config
  2. NSURLSession uses config to establish connections
  3. What type of request data is NSURLSession Task, up, down
  4. 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

  1. SURLSession can set up access to cellular non-access under wifi, etc.
  2. 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

  1. 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.
  2. 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).
  3. 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.
  4. 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

  5. NSURLSession versus NSURLConnection & Using Shared Session Manager/Session
  6. Use of NSURLSession in AFNetWorking(3.0)
  7. AFNetworking 3.0 Source Code Interpretation Series (Parsing)
  8. Learning summary after reading af

    Limited capacity. Welcome to the discussion.

Keywords: network Session iOS

Added by SecureMind on Wed, 03 Jul 2019 01:16:27 +0300