
??????Alamofire專題目錄,歡迎及時反饋交流 ??????
Alamofire 目錄直通車 --- 和諧學(xué)習(xí),不急不躁!
Alamofire請求數(shù)據(jù)之后,我們就會回調(diào)響應(yīng),但是底層是如何保證響應(yīng)必然在請求之后的呢?以及Alamofire的Response到底是什么東西,這一篇詳細講解。
一、Response
1:response的執(zhí)行順序
首先我們先來看這段代碼
SessionManager.default
.request(urlString)
.response { (response) in
print(response)
}
Alamofire 一個非常關(guān)鍵的類就是 Request ,請看下面這段代碼是鏈式調(diào)用,但是怎么保證 response 在 request 之后呢?


- 我們
response的任務(wù)是加入到了delegate.queue.addOperation - 交付給了主隊列,畢竟這里的
response是給用戶對外提供的,用戶可以直接UI操作 - 然后回到閉包出去
init(task: URLSessionTask?) {
_task = task
self.queue = {
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
operationQueue.isSuspended = true
operationQueue.qualityOfService = .utility
return operationQueue
}()
}
- 這個隊列的并發(fā)數(shù)為 1
- 初始化出來是默認掛起狀態(tài)

- 請求完成的時候:把隊列的掛起狀態(tài)取消了,那么這個時候就可以正常執(zhí)行任務(wù)
- 剛剛在加入到這個隊列里面的任務(wù)就可以在請求完成的時候順序執(zhí)行 Soga
2:response的作用
response 分為四種
- DefaultDataResponse
- DataResponse
- DefaultDownloadResponse
- DownloadResponse
這里可以看到并沒有 upload 相關(guān)的,為什么???那是因為 upload 返回的就是普通數(shù)據(jù),就沒有必要重新封裝。
其中 Default 開頭就是返回原始數(shù)據(jù),沒有進過其他處理,不加 Default 可以通過序列化器處理!大家可以對比下面兩個方法,不難得出結(jié)果


- 其實如果細心的你,???? 應(yīng)該很容易可以得出,其實這里封裝
Response和我們傳統(tǒng)的Response不是同一個。里封裝Response是一個數(shù)據(jù)儲存模型 ,里面保存對外所需要的數(shù)據(jù)
self.request = request
self.response = response
self.data = data
self.error = error
self.timeline = timeline
三、序列化器
就拿我們最熟悉的 json 序列化器來給大家一起討論
public func responseJSON(
queue: DispatchQueue? = nil,
options: JSONSerialization.ReadingOptions = .allowFragments,
completionHandler: @escaping (DataResponse<Any>) -> Void)
-> Self
{
return response(
queue: queue,
responseSerializer: DataRequest.jsonResponseSerializer(options: options),
completionHandler: completionHandler
)
}
- 這里封裝了一個
response的方法 - 第三個參數(shù)是序列化器的初始化
public static func jsonResponseSerializer(
options: JSONSerialization.ReadingOptions = .allowFragments)
-> DataResponseSerializer<Any>
{
return DataResponseSerializer { _, response, data, error in
return Request.serializeResponseJSON(options: options, response: response, data: data, error: error)
}
}
- 這里返回的就是
DataResponseSerializer類型的序列化器 - 其中參數(shù)就是一個閉包,這個閉包帶有一個返回值類型
Result:Request.serializeResponseJSON - 之前上面就是對這個初始化出來的
DataResponseSerializer的參數(shù)閉包的調(diào)用:DataRequest.jsonResponseSerializer(options: options)
public static func serializeResponseJSON(
options: JSONSerialization.ReadingOptions,
response: HTTPURLResponse?,
data: Data?,
error: Error?)
-> Result<Any>
{
// 省略了一些不重要的代碼
do {
let json = try JSONSerialization.jsonObject(with: validData, options: options)
return .success(json)
} catch {
return .failure(AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error)))
}
}
- 很簡單的封裝驗證了一些數(shù)據(jù)
- 然后就是非常熟悉的
json序列化器:JSONSerialization.jsonObject - 根據(jù)序列化的結(jié)果返回 :
.success(json)或者.failure(error)
四、總結(jié)
- 創(chuàng)建一個序列化結(jié)構(gòu)體
- 通過序列化結(jié)構(gòu)體 - 發(fā)起序列化響應(yīng)閉包
- 把外界就是
taskDelegate里面的數(shù)據(jù) -> 傳到我們外界的閉包 - 交給我們自定義的序列或者系統(tǒng)幫我們實現(xiàn)的序列化器實現(xiàn) -
response驗證 -response.statusCode判斷 - 發(fā)出result -
result就是我們的序列化器的返回值 - 同步
operation把result交給response結(jié)構(gòu)體 -
data/downloadResponse儲存數(shù)據(jù) -
response回調(diào)返回response響應(yīng)數(shù)據(jù)
非常高興我們霸占了簡書
RxSwift ,Alamofire板塊,只要搜索RxSwift ,Alamofire相關(guān)最新文章必然都是一些熟悉的身影~~~ 持續(xù)努力,變平凡為非凡 ?? ?? ??就問此時此刻還有誰?45度仰望天空,該死!我這無處安放的魅力!