AFN - AFURLSessionManager

非常棒的文章-AFURLSessionManager

AFURLSessionManager根據(jù)一個指定的NSURLSessionConfiguration創(chuàng)建和管理一個NSURLSession對象。并且這個對象實現(xiàn)了NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, 和 NSURLSessionDelegate這幾個協(xié)議的協(xié)議方法。同時實現(xiàn)NSSecureCoding和NSCopying來實現(xiàn)歸檔解檔和copy功能。

AFURLSessionManager通過對task設置一個AFURLSessionManagerTaskDelegate代理來處理請求進度管理,在iOS7和iOS8及以上的系統(tǒng),NSRULSessionTask的具體實現(xiàn)是不同的,所以用了runtime動態(tài)添加了af_resume和af_suspend并且替換了系統(tǒng)的方法

1、初始化API和屬性

指定的初始化方法、通過他來初始化一個Manager對象。傳nil默認是defaultSessionConfiguration,數(shù)據(jù)處理會在內(nèi)部創(chuàng)建OperationQueue

- (instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration 

AFURLSessionManager通過session來管理和創(chuàng)建網(wǎng)絡請求。一個manager就實現(xiàn)了對這個session的管理,他們是一一對應的關系。

@property (readonly, nonatomic, strong) NSURLSession *session;

處理網(wǎng)絡請求回調(diào)的操作隊列,就是我們初始化session的時候傳入的那個OperationQueue參數(shù)。如果不傳入,默認是MainOperationQueue。

@property (readonly, nonatomic, strong) NSOperationQueue *operationQueue;

對返回數(shù)據(jù)的處理都通過這個屬性來處理,比如數(shù)據(jù)的提取、轉(zhuǎn)換等。默認是一個AFJSONResponseSerializer對象用JSON的方式解析。

@property (nonatomic, strong) id <AFURLResponseSerialization> responseSerializer;

用于指定session的安全策略。用于處理信任主機和證書認證等。默認是defaultPolicy。

@property (nonatomic, strong) AFSecurityPolicy *securityPolicy;

觀測網(wǎng)絡狀態(tài)的變化

@property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager;

2、獲取Task

當前session創(chuàng)建的各種類型tasks

@property (readonly, nonatomic, strong) NSArray <NSURLSessionTask *> *tasks;
@property (readonly, nonatomic, strong) NSArray <NSURLSessionDataTask *> *dataTasks;
@property (readonly, nonatomic, strong) NSArray <NSURLSessionUploadTask *> *uploadTasks;
@property (readonly, nonatomic, strong) NSArray <NSURLSessionDownloadTask *> *downloadTasks;

用于處理任務回調(diào)的GCD的group對象,如果不初始化、則一個默認的Group被使用。

@property (nonatomic, strong, nullable) dispatch_group_t completionGroup;

用于處理任務回調(diào)的GCD對象,默認是dispatch_main_queue。

@property (nonatomic, strong, nullable) dispatch_queue_t completionQueue;

在iOS7的環(huán)境下,我們通過background模式的session創(chuàng)建的uploadTask有時會是nil,如果這個屬性是yes,AFN會嘗試再次創(chuàng)建uploadTask。

@property (nonatomic, assign) BOOL attemptsToRecreateUploadTasksForBackgroundSessions;

廢除manager對應的Session。通過傳入的參數(shù)來決定是否立即取消已經(jīng)用session發(fā)出去的任務。

- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks;

3、管理Task創(chuàng)建Block

他把所有delegate方法細節(jié)都處理好。直接提供給我們一些最實用的api,我們就不用去管理session系列繁瑣的delegate方法了.

創(chuàng)建一個NSURLSessionDataTask

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request 
completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler;

創(chuàng)建一個NSURLSessionDataTask,并且能獲取上傳或者下載進度

- (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;

創(chuàng)建一個上傳Task,并且指定上傳文件的路徑。獲取上傳進度

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
 fromFile:(NSURL *)fileURL
 progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock
 completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError  * _Nullable error))completionHandler;

創(chuàng)建一個上傳Task,并且指定上傳的數(shù)據(jù)。獲取上傳進度

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
 fromData:(nullable NSData *)bodyData
  progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock
 completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler;

創(chuàng)建一個uploadTask,(表單形式)然后上傳數(shù)據(jù)

- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request
 progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock
 completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler;

新建一個download任務,destination表示的下載文件的緩存路徑

- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
                                             progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock
                                          destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
                                    completionHandler:(nullable void (^)(NSURLResponse *response, NSURL * _Nullable filePath, NSError * _Nullable error))completionHandler;

繼續(xù)恢復一個download任務。resumeData參數(shù)表示的是恢復下載的時候初始化數(shù)據(jù),比如前面已經(jīng)下載好的部分數(shù)據(jù)。

- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData
                                                progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock
                                             destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
                                       completionHandler:(nullable void (^)(NSURLResponse *response, NSURL * _Nullable filePath, NSError * _Nullable error))completionHandler;

獲取指定Task的上傳進度

- (nullable NSProgress *)uploadProgressForTask:(NSURLSessionTask *)task;

獲取指定Task的下載進度

- (nullable NSProgress *)downloadProgressForTask:(NSURLSessionTask *)task;

AFURLSessionManager設置各種情況的代理回調(diào)

代理方法Block.png

這些方法是對NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, 和 NSURLSessionDelegate這幾個協(xié)議的協(xié)議方法的block形式的實現(xiàn)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容