本篇主要講解Alamofire中如何把服務(wù)器返回的數(shù)據(jù)序列化
前言
和前邊的文章不同, 在這一篇中卤恳,我想從程序的設(shè)計層次上解讀ResponseSerialization這個文件酝静。更直觀的去探討該功能是如何一步一步實現(xiàn)的览祖。當(dāng)然靡努,有一個不好的地方摊崭,跟數(shù)學(xué)問題一樣讼油,我們事先知道了結(jié)果,因此這是一個已知結(jié)果推到過程的問題爽室。
在之前Alamofire的源碼解讀文章中汁讼,我們已經(jīng)知道了:對于響應(yīng)感興趣的Request類型是DataRequest和DownloadRequest淆攻。我們下邊所有的設(shè)計都是針對這兩個類型的請求的阔墩。
不序列化的設(shè)計
我們先從最簡單的事情著手。如果我發(fā)起了一個請求瓶珊,我肯定希望知道請求的結(jié)果啸箫,那么就會有下邊這樣的偽代碼:
dataRequest.request().response{ ResponseObj in }
downloadRequest.request().response{ ResponseObj in }
上邊的偽代碼中的response函數(shù)是請求的回調(diào)函數(shù),ResponseObj是對服務(wù)器返回的數(shù)據(jù)的一個抽象伞芹。這就完成了最基本的需求忘苛。
默認(rèn)情況下我們可能希望回調(diào)函數(shù)會在主線程調(diào)用蝉娜,但是對于某些特定的功能,還是應(yīng)該增加對多線程的支持扎唾,因此我們把上邊的代碼做一下擴展:
dataRequest.request().response(queue 回調(diào)函數(shù))
downloadRequest.request().response(queue 回調(diào)函數(shù))
給response函數(shù)增加一個參數(shù)召川,這個參數(shù)用來決定回調(diào)函數(shù)會在哪個線程被調(diào)用。這里的回調(diào)函數(shù)會給我們一個需要的結(jié)果胸遇,在Alamofire中荧呐,DataRequest對應(yīng)的結(jié)果是DefaultDataResponse,DownloadRequest對應(yīng)的結(jié)果是DefaultDownloadResponse纸镊。
因此倍阐,我們把上邊的偽代碼還原成Alamfire中的函數(shù)就是:
@discardableResult
public func response(queue: DispatchQueue? = nil, completionHandler: @escaping (DefaultDataResponse) -> Void) -> Self {
delegate.queue.addOperation {
(queue ?? DispatchQueue.main).async {
var dataResponse = DefaultDataResponse(
request: self.request,
response: self.response,
data: self.delegate.data,
error: self.delegate.error,
timeline: self.timeline
)
dataResponse.add(self.delegate.metrics)
completionHandler(dataResponse)
}
}
return self
}
@discardableResult
public func response(
queue: DispatchQueue? = nil,
completionHandler: @escaping (DefaultDownloadResponse) -> Void)
-> Self
{
delegate.queue.addOperation {
(queue ?? DispatchQueue.main).async {
var downloadResponse = DefaultDownloadResponse(
request: self.request,
response: self.response,
temporaryURL: self.downloadDelegate.temporaryURL,
destinationURL: self.downloadDelegate.destinationURL,
resumeData: self.downloadDelegate.resumeData,
error: self.downloadDelegate.error,
timeline: self.timeline
)
downloadResponse.add(self.delegate.metrics)
completionHandler(downloadResponse)
}
}
return self
}
這兩個函數(shù)都是把先創(chuàng)建Response對象,然后把這些操作放入到delegate的隊列中逗威,當(dāng)請求完成后再執(zhí)行這些operation峰搪。
需要序列化
那么問題就來了,在未序列化的基礎(chǔ)上應(yīng)該如何添加序列化功能?在Alamofire源碼解讀系列(九)之響應(yīng)封裝(Response)這一篇文章中我們知道針對序列化的Response有兩個封裝:DataResponse和DownloadResponse。他們都是struct眯勾,是純正的存儲設(shè)計屬性形导。和DefaultDataResponse,DefaultDownloadResponse最大的不同饵骨,其內(nèi)部多了一個Result的封裝。不明白Result的朋友可以去看看這篇文章Alamofire源碼解讀系列(五)之結(jié)果封裝(Result).
因此只要在上邊的response方法中添加一個參數(shù)就行,這個參數(shù)的任務(wù)就是完成數(shù)據(jù)的序列化春锋。此時我們說的系列化就是指可以把響應(yīng)數(shù)據(jù)生成Result的功能。因為DataResponse和DownloadResponse的初始化離不開這個參數(shù)差凹。
偽代碼如下:
dataRequest.request().response(queue 序列化者 回調(diào)函數(shù))
downloadRequest.request().response(queue 序列化者 回調(diào)函數(shù))
我們之所以把data和download的請求每次都分開來設(shè)計期奔,原因是因為這兩個不同的請求得到的響應(yīng)不一樣。download可以從一個URL中獲取數(shù)據(jù)危尿,而data不行呐萌。
那么重點來了,序列化者的任務(wù)是把數(shù)據(jù)轉(zhuǎn)換成Result谊娇。因此我們可以把這個序列化者設(shè)計成一個類或者結(jié)構(gòu)體肺孤,里邊提供一個轉(zhuǎn)換的方法就行了。這也是最正常不過的思想济欢。但是在swift中我們應(yīng)該轉(zhuǎn)變思維赠堵。swift跟oc不一樣。
我們不應(yīng)該把系列化者用一個固定的對象封死法褥。這個時候就是協(xié)議大顯身手的時刻了茫叭。既然序列化者需要一個函數(shù),那么我們就設(shè)計一個包含該函數(shù)的協(xié)議半等。這一切的思想應(yīng)該是從高層到底層的過度的揍愁。因此協(xié)議就是下邊的代碼:
/// The type in which all data response serializers must conform to in order to serialize a response.
public protocol DataResponseSerializerProtocol {
/// The type of serialized object to be created by this `DataResponseSerializerType`.
associatedtype SerializedObject
/// A closure used by response handlers that takes a request, response, data and error and returns a result.
var serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<SerializedObject> { get }
}
/// The type in which all download response serializers must conform to in order to serialize a response.
public protocol DownloadResponseSerializerProtocol {
/// The type of serialized object to be created by this `DownloadResponseSerializerType`.
associatedtype SerializedObject
/// A closure used by response handlers that takes a request, response, url and error and returns a result.
var serializeResponse: (URLRequest?, HTTPURLResponse?, URL?, Error?) -> Result<SerializedObject> { get }
}
SerializedObject定義了要序列化后的對象類型呐萨,這么寫的原因也是因為后邊序列成Data,JOSN莽囤,String等等的需求谬擦。在回到序列者的問題上,只要實現(xiàn)了這些協(xié)議就行朽缎,序列者應(yīng)該是一個存儲屬性怯屉,用序列化函數(shù)作為參數(shù)來初始化:
/// A generic `DataResponseSerializerType` used to serialize a request, response, and data into a serialized object.
public struct DataResponseSerializer<Value>: DataResponseSerializerProtocol {
/// The type of serialized object to be created by this `DataResponseSerializer`.
public typealias SerializedObject = Value
/// A closure used by response handlers that takes a request, response, data and error and returns a result.
public var serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<Value>
/// Initializes the `ResponseSerializer` instance with the given serialize response closure.
///
/// - parameter serializeResponse: The closure used to serialize the response.
///
/// - returns: The new generic response serializer instance.
public init(serializeResponse: @escaping (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<Value>) {
self.serializeResponse = serializeResponse
}
}
/// A generic `DownloadResponseSerializerType` used to serialize a request, response, and data into a serialized object.
public struct DownloadResponseSerializer<Value>: DownloadResponseSerializerProtocol {
/// The type of serialized object to be created by this `DownloadResponseSerializer`.
public typealias SerializedObject = Value
/// A closure used by response handlers that takes a request, response, url and error and returns a result.
public var serializeResponse: (URLRequest?, HTTPURLResponse?, URL?, Error?) -> Result<Value>
/// Initializes the `ResponseSerializer` instance with the given serialize response closure.
///
/// - parameter serializeResponse: The closure used to serialize the response.
///
/// - returns: The new generic response serializer instance.
public init(serializeResponse: @escaping (URLRequest?, HTTPURLResponse?, URL?, Error?) -> Result<Value>) {
self.serializeResponse = serializeResponse
}
}
@discardableResult
public func response<T: DataResponseSerializerProtocol>(
queue: DispatchQueue? = nil,
responseSerializer: T,
completionHandler: @escaping (DataResponse<T.SerializedObject>) -> Void)
-> Self
{
delegate.queue.addOperation {
/// 這里就調(diào)用了responseSerializer保存的系列化函數(shù),函數(shù)調(diào)用后會得到result
let result = responseSerializer.serializeResponse(
self.request,
self.response,
self.delegate.data,
self.delegate.error
)
/// 這里一定要記得饵沧,DataResponse是一個結(jié)構(gòu)體锨络,是專門為了純存儲數(shù)據(jù)的,這里是調(diào)用了結(jié)構(gòu)體的初始化方法創(chuàng)建了一個新的DataResponse實例
var dataResponse = DataResponse<T.SerializedObject>(
request: self.request,
response: self.response,
data: self.delegate.data,
result: result,
timeline: self.timeline
)
dataResponse.add(self.delegate.metrics)
(queue ?? DispatchQueue.main).async { completionHandler(dataResponse) }
}
return self
}
@discardableResult
public func response<T: DownloadResponseSerializerProtocol>(
queue: DispatchQueue? = nil,
responseSerializer: T,
completionHandler: @escaping (DownloadResponse<T.SerializedObject>) -> Void)
-> Self
{
delegate.queue.addOperation {
let result = responseSerializer.serializeResponse(
self.request,
self.response,
self.downloadDelegate.fileURL,
self.downloadDelegate.error
)
var downloadResponse = DownloadResponse<T.SerializedObject>(
request: self.request,
response: self.response,
temporaryURL: self.downloadDelegate.temporaryURL,
destinationURL: self.downloadDelegate.destinationURL,
resumeData: self.downloadDelegate.resumeData,
result: result,
timeline: self.timeline
)
downloadResponse.add(self.delegate.metrics)
(queue ?? DispatchQueue.main).async { completionHandler(downloadResponse) }
}
return self
}
擴展
其實狼牺,代碼到了這里羡儿,基本的功能已經(jīng)完成了80%。如果要把data序列成string是钥,只需要創(chuàng)建一個data序列者就好了掠归,但是這樣的設(shè)計用起來很麻煩,因為還要書寫序列成Result的函數(shù)悄泥,這些函數(shù)往往都是一樣的虏冻,要么把這些函數(shù)提前定義出來,要么把這些函數(shù)封裝起來弹囚。
按照Alamofire的設(shè)計厨相,是把這些函數(shù)封裝起來的。你可以這么使用:
dataRequest.request().responseString(queue 回調(diào)函數(shù))
dataRequest.request().responseJSON(queue 回調(diào)函數(shù))
通過特性的函數(shù)來獲取序列化后的response鸥鹉。
responseData
responseData是把數(shù)據(jù)序列化為Data類型蛮穿。也就是Result<Data>。
生成DataRequest的序列者:
/// Creates a response serializer that returns the associated data as-is.
///
/// - returns: A data response serializer.
public static func dataResponseSerializer() -> DataResponseSerializer<Data> {
/// 可以看出這么寫也是可以的毁渗,這個方法要做分解才能理解践磅,不然很容易讓人迷惑,DataResponseSerializer的初始化需要一個ResponseSerializer函數(shù)灸异,那么這個函數(shù)是什么呢府适?就是大括號內(nèi)部的這個閉包,我們通過下邊的代碼就得到了一個DataResponseSerializer肺樟,這個DataResponseSerializer內(nèi)部保存著一個函數(shù)檐春,函數(shù)的作用就是根據(jù)參數(shù),最終解析出Result<Data>
// return DataResponseSerializer { (_, response, data, error) -> Result<Data> in
// return Request.serializeResponseData(response: response, data: data, error: error)
// }
return DataResponseSerializer { _, response, data, error in
return Request.serializeResponseData(response: response, data: data, error: error)
}
}
實現(xiàn)DataRequest的responseData函數(shù):
/// Adds a handler to be called once the request has finished.
///
/// - parameter completionHandler: The code to be executed once the request has finished.
///
/// - returns: The request.
/// 這個方法就很好裂解了 儡嘶,設(shè)置一個回調(diào)喇聊,當(dāng)請求完成調(diào)用,
@discardableResult
public func responseData(
queue: DispatchQueue? = nil,
completionHandler: @escaping (DataResponse<Data>) -> Void)
-> Self
{
return response(
queue: queue,
responseSerializer: DataRequest.dataResponseSerializer(),
completionHandler: completionHandler
)
}
生成DownloadRequest的序列者:
/// Creates a response serializer that returns the associated data as-is.
///
/// - returns: A data response serializer.
public static func dataResponseSerializer() -> DownloadResponseSerializer<Data> {
return DownloadResponseSerializer { _, response, fileURL, error in
guard error == nil else { return .failure(error!) }
guard let fileURL = fileURL else {
return .failure(AFError.responseSerializationFailed(reason: .inputFileNil))
}
do {
let data = try Data(contentsOf: fileURL)
return Request.serializeResponseData(response: response, data: data, error: error)
} catch {
return .failure(AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL)))
}
}
}
實現(xiàn)DataRequest的responseData函數(shù):
/// Adds a handler to be called once the request has finished.
///
/// - parameter completionHandler: The code to be executed once the request has finished.
///
/// - returns: The request.
@discardableResult
public func responseData(
queue: DispatchQueue? = nil,
completionHandler: @escaping (DownloadResponse<Data>) -> Void)
-> Self
{
return response(
queue: queue,
responseSerializer: DownloadRequest.dataResponseSerializer(),
completionHandler: completionHandler
)
}
上邊的代碼中值得注意的是:初始化序列者需要的是一個函數(shù)蹦狂,只要把這個函數(shù)看做是一個參數(shù)誓篱,就能明白為什么會這么寫。那么我們更應(yīng)該關(guān)心的是下邊的函數(shù)凯楔,它解釋了如何根據(jù)response: HTTPURLResponse?, data: Data?, error: Error?獲得Result<Data>窜骄。也是序列化Data的核心方法:
/// Returns a result data type that contains the response data as-is.
///
/// - parameter response: The response from the server.
/// - parameter data: The data returned from the server.
/// - parameter error: The error already encountered if it exists.
///
/// - returns: The result data type.
public static func serializeResponseData(response: HTTPURLResponse?, data: Data?, error: Error?) -> Result<Data> {
guard error == nil else { return .failure(error!) }
if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(Data()) }
guard let validData = data else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))
}
return .success(validData)
}
responseString
responseString跟responseData的套路一模一樣,就不把全部的代碼弄過來了摆屯,以免浪費篇幅邻遏,我們應(yīng)該關(guān)心如何根據(jù)encoding: String.Encoding?,response: HTTPURLResponse?,data: Data?,error: Error?獲得Result<String>。
/// Returns a result string type initialized from the response data with the specified string encoding.
///
/// - parameter encoding: The string encoding. If `nil`, the string encoding will be determined from the server
/// response, falling back to the default HTTP default character set, ISO-8859-1.
/// - parameter response: The response from the server.
/// - parameter data: The data returned from the server.
/// - parameter error: The error already encountered if it exists.
///
/// - returns: The result data type.
public static func serializeResponseString(
encoding: String.Encoding?,
response: HTTPURLResponse?,
data: Data?,
error: Error?)
-> Result<String>
{
guard error == nil else { return .failure(error!) }
if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success("") }
guard let validData = data else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))
}
var convertedEncoding = encoding
if let encodingName = response?.textEncodingName as CFString!, convertedEncoding == nil {
convertedEncoding = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(
CFStringConvertIANACharSetNameToEncoding(encodingName))
)
}
let actualEncoding = convertedEncoding ?? String.Encoding.isoLatin1
if let string = String(data: validData, encoding: actualEncoding) {
return .success(string)
} else {
return .failure(AFError.responseSerializationFailed(reason: .stringSerializationFailed(encoding: actualEncoding)))
}
}
上邊的代碼中涉及了字符串編碼的知識虐骑,有興趣的朋友可以自己查找資料准验。
responseJSON
responseJSON跟responseData的套路一模一樣,就不把全部的代碼弄過來了廷没,以免浪費篇幅糊饱,我們應(yīng)該關(guān)心如何根據(jù)options: JSONSerialization.ReadingOptions,response: HTTPURLResponse?,data: Data?,error: Error?獲得Result<Any>。
/// Returns a JSON object contained in a result type constructed from the response data using `JSONSerialization`
/// with the specified reading options.
///
/// - parameter options: The JSON serialization reading options. Defaults to `.allowFragments`.
/// - parameter response: The response from the server.
/// - parameter data: The data returned from the server.
/// - parameter error: The error already encountered if it exists.
///
/// - returns: The result data type.
public static func serializeResponseJSON(
options: JSONSerialization.ReadingOptions,
response: HTTPURLResponse?,
data: Data?,
error: Error?)
-> Result<Any>
{
guard error == nil else { return .failure(error!) }
if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(NSNull()) }
guard let validData = data, validData.count > 0 else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength))
}
do {
let json = try JSONSerialization.jsonObject(with: validData, options: options)
return .success(json)
} catch {
return .failure(AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error)))
}
}
這里之所以使用Any颠黎,是因為JSON可能是字典另锋,也可能是數(shù)組。
responsePropertyList
responsePropertyList跟responseData的套路一模一樣狭归,就不把全部的代碼弄過來了夭坪,以免浪費篇幅,我們應(yīng)該關(guān)心如何根據(jù)options: PropertyListSerialization.ReadOptions,response: HTTPURLResponse?,data: Data?,error: Error?獲得Result<Any>过椎。
/// Returns a plist object contained in a result type constructed from the response data using
/// `PropertyListSerialization` with the specified reading options.
///
/// - parameter options: The property list reading options. Defaults to `[]`.
/// - parameter response: The response from the server.
/// - parameter data: The data returned from the server.
/// - parameter error: The error already encountered if it exists.
///
/// - returns: The result data type.
public static func serializeResponsePropertyList(
options: PropertyListSerialization.ReadOptions,
response: HTTPURLResponse?,
data: Data?,
error: Error?)
-> Result<Any>
{
guard error == nil else { return .failure(error!) }
if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(NSNull()) }
guard let validData = data, validData.count > 0 else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength))
}
do {
let plist = try PropertyListSerialization.propertyList(from: validData, options: options, format: nil)
return .success(plist)
} catch {
return .failure(AFError.responseSerializationFailed(reason: .propertyListSerializationFailed(error: error)))
}
}
emptyDataStatusCodes
如果HTTP response code 是204或者205室梅,就表示Data為nil。
/// A set of HTTP response status code that do not contain response data.
private let emptyDataStatusCodes: Set<Int> = [204, 205]
為Request添加Timeline屬性
extension Request {
var timeline: Timeline {
let requestCompletedTime = self.endTime ?? CFAbsoluteTimeGetCurrent()
let initialResponseTime = self.delegate.initialResponseTime ?? requestCompletedTime
return Timeline(
requestStartTime: self.startTime ?? CFAbsoluteTimeGetCurrent(),
initialResponseTime: initialResponseTime,
requestCompletedTime: requestCompletedTime,
serializationCompletedTime: CFAbsoluteTimeGetCurrent()
)
}
}
上邊的代碼為Request添加了Timeline屬性疚宇,這是一個計算屬性竞惋,因此在不同的請求階段會獲得不同的取值。
總結(jié)
由于知識水平有限灰嫉,如有錯誤拆宛,還望指出
鏈接
Alamofire源碼解讀系列(一)之概述和使用 簡書-----博客園
Alamofire源碼解讀系列(二)之錯誤處理(AFError) 簡書-----博客園
Alamofire源碼解讀系列(三)之通知處理(Notification) 簡書-----博客園
Alamofire源碼解讀系列(四)之參數(shù)編碼(ParameterEncoding) 簡書-----博客園
Alamofire源碼解讀系列(五)之結(jié)果封裝(Result) 簡書-----博客園
Alamofire源碼解讀系列(六)之Task代理(TaskDelegate) 簡書-----博客園
Alamofire源碼解讀系列(七)之網(wǎng)絡(luò)監(jiān)控(NetworkReachabilityManager) 簡書-----博客園