iOS 深度好文

AFNetworking源码分析

2021-06-25  本文已影响0人  云中追月

一、概述

AFNetworking代码结构

从上图可以发现,AFNetworking框架的结构比较清晰简洁,主要范围5个部分:

二、AFNetworking使用

例子:

AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json" ,@"text/javascript", nil];
session.responseSerializer = [AFHTTPResponseSerializer serializer];
[session GET:@"https://www.baidu.com"
  parameters:nil
     headers:nil
    progress:nil
     success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSLog(@"请求成功");
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
          NSLog(@"请求失败");
     }];

三、AFNetworking使用的代码分析

3.1 [AFHTTPSessionManager manager]

AFHTTPSessionManager is a subclass of AFURLSessionManager with convenience methods for making HTTP requests. When a base URL is provided, requests made with the GET/POST/ et al. convenience methods can be made with relative paths.

AFHTTPSessionManager manage的调用栈
AFHTTPSessionManager manage的实现
+ (instancetype)manager {
    return [[[self class] alloc] initWithBaseURL:nil];
}

- (instancetype)init {
    return [self initWithBaseURL:nil];
}

- (instancetype)initWithBaseURL:(NSURL *)url {
    return [self initWithBaseURL:url sessionConfiguration:nil];
}

- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
    return [self initWithBaseURL:nil sessionConfiguration:configuration];
}

- (instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
    self = [super initWithSessionConfiguration:configuration];
    if (!self) {
        return nil;
    }
    
    if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
        url = [url URLByAppendingPathComponent:@""];
    }
    
    self.baseURL = url;
    
    self.requestSerializer = [AFHTTPRequestSerializer serializer];
    self.responseSerializer = [AFJSONResponseSerializer serializer];
    
    return self;
}

进入到AFURLSessionManager- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration方法中分析

- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
    self = [super init];
    if (!self) {
        return nil;
    }

    if (!configuration) {
        configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    }
// 1. 设置全局的网络行为策略的配置
    self.sessionConfiguration = configuration;
// 2. 设置请求的队列,默认最大的并发数为1
    self.operationQueue = [[NSOperationQueue alloc] init];
// (1)这里的并发数指的是回调代理的线程并发数,而不是请求网络的线程并发数。请求网络是由NSURLSession来做的,它内部维护了一个线程池用来做网络请求。NSURLSession调度线程是基于底层的CFSocket去发送请求和接收数据,这些线程是并发的。
// (2)AF2.x所有回调是在一条线程,这条线程是AF的常驻线程,而这一条线程正式AF调度request的思想精髓所在。所以线程数设置为1的第一个目的是和之前版本保持一致;
// (3)因为跟代理相关的一些操作AF都使用了NSLock,所以Queue的并发数设置为n,也会因为多线程回调、锁的等待,导致提升的程序速度并不明显,反而多task回调导致的多线程并发,平白浪费了部分性能。至少回调的事件,是不需要多线程并发的,回调没有了NSLock的等待事件,所以对事件并没有多大影响。
    self.operationQueue.maxConcurrentOperationCount = 1;
// 3. 设置网络请求响应的数据解析实例
    self.responseSerializer = [AFJSONResponseSerializer serializer];
// 4. 设置网络请求安全策略实例(后续说明该实例)
    self.securityPolicy = [AFSecurityPolicy defaultPolicy];
// 5. 初始化全局的网络状态监听的实例
#if !TARGET_OS_WATCH
    self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
#endif
// 6. 将taskID与其Delegate绑定,实现解耦(后续进行分析)
    self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init];

    self.lock = [[NSLock alloc] init];
    self.lock.name = AFURLSessionManagerLockName;
// 7. self.session采用懒加载,根据configuration,operationQueue初始化全局的NSURLSession
    [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
        for (NSURLSessionDataTask *task in dataTasks) {
            [self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
        }

        for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
            [self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
        }

        for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
            [self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
        }
    }];

    return self;
}

- (NSURLSession *)session {
    
    @synchronized (self) {
        if (!_session) {
            _session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
        }
    }
    return _session;
}

3.2 NSURLSessionConfiguration 全局的网络行为策略的配置

An NSURLSessionConfiguration object defines the behavior and policies to use when uploading and downloading data using an NSURLSession object. When uploading or downloading data, creating a configuration object is always the first step you must take. You use this object to configure the timeout values, caching policies, connection requirements, and other types of information that you intend to use with your NSURLSession object.

简单罗列以下两点:
1.NSURLSessionConfiguration可以控制网络请求中的缓存策略,超时设置等;
2.如果需要改变网络请求的行为策略,必须重新在更改NSURLSessionConfiguration后再创建一个新的NSURLSession。

3.3 AFHTTPSessionManager GET: parameters: headers: progress: success: failure: 方法

AFHTTPSessionManager GET: parameters: headers: progress: success: failure: 方法调用栈

3.3.1 NSURLSessionTask

这里引入了一个新的类:NSURLSessionDataTaskNSURLSessionDataTask继承了NSURLSessionTask。** NSURLSessionTask**的官方说明:

The NSURLSessionTask class is the base class for tasks in a URL session. Tasks are always part of a session; you create a task by calling one of the task creation methods on an NSURLSession object. The method you call determines the type of task.
URL sessions provide three types of tasks: data tasks, upload tasks, and download tasks. These tasks are instances of the NSURLSessionDataTask, NSURLSessionUploadTask, NSURLSessionDownloadTask, NSURLSessionStreamTask subclasses of NSURLSessionTask, respectively.

简单罗列以下两点:
1.NSURLSessionTask是官方提供的几种网络任务类的基类;
2.官方提供了三种任务处理的子类:简单数据处理任务类、上传任务类和下载任务类。

3.3.2 AFHTTPSessionManager dataTaskWithHTTPMethod:...方法实现与分析

- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(id)parameters
                                         headers:(NSDictionary<NSString *,NSString *> *)headers
                                  uploadProgress:(void (^)(NSProgress * _Nonnull))uploadProgress
                                downloadProgress:(void (^)(NSProgress * _Nonnull))downloadProgress
                                         success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
                                         failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure {
    NSError *serializationError = nil;
// 1.通过全局配置的requestSerializer初始化一个请求的实例
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];

    for (NSString *headerField in headers.keyEnumerator) {
        [request setValue:headers[headerField] forKey:headerField];
    }
    
    if (serializationError) {
        if (failure) {
            dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
                failure(nil, serializationError);
            });
        }
        return nil;
    }
    
    // 2.根据请求的实例再初始化一个task的实例
    __block NSURLSessionDataTask *dataTask = nil;
    dataTask = [self dataTaskWithRequest:request
                          uploadProgress:uploadProgress
                        downloadProgress:downloadProgress
                       completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
        if (error) {
            if (failure) {
                failure(dataTask, error);
            }
        } else {
            if (success) {
                success(dataTask, responseObject);
            }
        }
    }];

    return dataTask;
}

3.3.3 [AFURLSessionManager addDelegateForDataTask:...]方法,用于task与delegate的绑定

// 1. 根据请求信息初始化一个task实例
- (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 {

    NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:request];

    [self addDelegateForDataTask:dataTask uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler];

    return dataTask;
}


- (void)addDelegateForDataTask:(NSURLSessionDataTask *)dataTask
                uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
              downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
             completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
// 2.初始化一个任务的代理
    AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] initWithTask:dataTask];
    delegate.manager = self;

// 此处赋值了请求完成的回调,后续会用到
    delegate.completionHandler = completionHandler;
// 3.利用全局的字典存储绑定信息,key为taskID,value为代理的实例。
    dataTask.taskDescription = self.taskDescriptionForSessionTasks;
    [self setDelegate:delegate forTask:dataTask];

    delegate.uploadProgressBlock = uploadProgressBlock;
    delegate.downloadProgressBlock = downloadProgressBlock;
}

- (void)setDelegate:(AFURLSessionManagerTaskDelegate *)delegate
            forTask:(NSURLSessionTask *)task
{
    NSParameterAssert(task);
    NSParameterAssert(delegate);

    [self.lock lock];
// 1.绑定taskID与代理self.mutableTaskDelegatesKeyedByTaskIdentifier[@(task.taskIdentifier)] = delegate;
// 2.使用KVO对一些方法监听,返回上传或下载的进度
    self.mutableTaskDelegatesKeyedByTaskIdentifier[@(task.taskIdentifier)] = delegate;
    [self addNotificationObserverForTask:task];
    [self.lock unlock];
}

3.3.4 NSURLSessionTaskDelegate协议中的URLSession:task:didCompleteWithError:方法

NSURLSessionTaskDelegate协议包含NSURLSessionDelegate协议
当请求收到了响应后,会触发- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error;回调(AFURLSessionManager的session属性懒加载时,设置了delegate为self,会触发该代理的方法),以下是方法中的具体处理。

// AFURLSessionManager.m中NSURLSessionTaskDelegate方法的实现,该方法由系统自动触发
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
// 1.根据task获取绑定的代理实例
    AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:task];

    // delegate may be nil when completing a task in the background
    if (delegate) {
// 2.统一处理,调用AFURLSessionManagerTaskDelegate的同名方法。
        [delegate URLSession:session task:task didCompleteWithError:error];

        [self removeDelegateForTask:task];
    }

    if (self.taskDidComplete) {
        self.taskDidComplete(session, task, error);
    }
}

// AFURLSessionManagerTaskDelegate.m中NSURLSessionTaskDelegate方法的实现,该方法在AFURLSessionManager中通过delegate调用。
- (void)URLSession:(__unused NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    error = objc_getAssociatedObject(task, AuthenticationChallengeErrorKey) ?: error;
    __strong AFURLSessionManager *manager = self.manager;

    __block id responseObject = nil;

    NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
    userInfo[AFNetworkingTaskDidCompleteResponseSerializerKey] = manager.responseSerializer;

    //Performance Improvement from #2672
    NSData *data = nil;
    if (self.mutableData) {
        data = [self.mutableData copy];
        //We no longer need the reference, so nil it out to gain back some memory.
        self.mutableData = nil;
    }

#if AF_CAN_USE_AT_AVAILABLE && AF_CAN_INCLUDE_SESSION_TASK_METRICS
    if (@available(iOS 10, macOS 10.12, watchOS 3, tvOS 10, *)) {
        if (self.sessionTaskMetrics) {
            userInfo[AFNetworkingTaskDidCompleteSessionTaskMetrics] = self.sessionTaskMetrics;
        }
    }
#endif

    if (self.downloadFileURL) {
        userInfo[AFNetworkingTaskDidCompleteAssetPathKey] = self.downloadFileURL;
    } else if (data) {
        userInfo[AFNetworkingTaskDidCompleteResponseDataKey] = data;
    }

    if (error) {
// 1.iOS网络框架返回的错误信息处理
        userInfo[AFNetworkingTaskDidCompleteErrorKey] = error;

        dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
            if (self.completionHandler) {
                self.completionHandler(task.response, responseObject, error);
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
            });
        });
    } else {
// 2.请求成功后需要用响应的数据解析类的responseSerializer实例处理返回的数据
// 数据通过NSURLSessionDataDelegate协议的- (void)URLSession:(__unused NSURLSession *)session dataTask:(__unused NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data方法返回
        dispatch_async(url_session_manager_processing_queue(), ^{
            NSError *serializationError = nil;
            responseObject = [manager.responseSerializer responseObjectForResponse:task.response data:data error:&serializationError];

            if (self.downloadFileURL) {
                responseObject = self.downloadFileURL;
            }

            if (responseObject) {
                userInfo[AFNetworkingTaskDidCompleteSerializedResponseKey] = responseObject;
            }

            if (serializationError) {
                userInfo[AFNetworkingTaskDidCompleteErrorKey] = serializationError;
            }

            dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
// 3.绑定task与处理的数据代理时,传入代理的完成的回调
                if (self.completionHandler) {
                    self.completionHandler(task.response, responseObject, serializationError);
                }

                dispatch_async(dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
                });
            });
        });
    }
}

参考来源:
https://toutiao.io/posts/dibcw7/preview
https://www.cnblogs.com/ederwin/articles/10592839.html

上一篇下一篇

猜你喜欢

热点阅读