Alamofire源碼解讀系列(十)之序列化(ResponseSerialization)

本篇主要講解Alamofire中如何把服務器返回的數據序列化html

前言

和前邊的文章不一樣, 在這一篇中,我想從程序的設計層次上解讀ResponseSerialization這個文件。更直觀的去探討該功能是如何一步一步實現的。固然,有一個很差的地方,跟數學問題同樣,咱們事先知道告終果,所以這是一個已知結果推到過程的問題。json

在以前Alamofire的源碼解讀文章中,咱們已經知道了:對於響應感興趣的Request類型是DataRequest和DownloadRequest。咱們下邊全部的設計都是針對這兩個類型的請求的。swift

不序列化的設計

咱們先從最簡單的事情着手。若是我發起了一個請求,我確定但願知道請求的結果,那麼就會有下邊這樣的僞代碼:api

dataRequest.request().response{ ResponseObj in }
downloadRequest.request().response{ ResponseObj in }

上邊的僞代碼中的response函數是請求的回調函數,ResponseObj是對服務器返回的數據的一個抽象。這就完成了最基本的需求。數組

默認狀況下咱們可能但願回調函數會在主線程調用,可是對於某些特定的功能,仍是應該增長對多線程的支持,所以咱們把上邊的代碼作一下擴展:安全

dataRequest.request().response(queue 回調函數)
downloadRequest.request().response(queue 回調函數)

給response函數增長一個參數,這個參數用來決定回調函數會在哪一個線程被調用。這裏的回調函數會給咱們一個須要的結果,在Alamofire中,DataRequest對應的結果是DefaultDataResponse,DownloadRequest對應的結果是DefaultDownloadResponse。服務器

所以,咱們把上邊的僞代碼還原成Alamfire中的函數就是:網絡

@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
    }

這兩個函數都是把先建立Response對象,而後把這些操做放入到delegate的隊列中,當請求完成後再執行這些operation。多線程

須要序列化

那麼問題就來了,在未序列化的基礎上應該如何添加序列化功能?在Alamofire源碼解讀系列(九)之響應封裝(Response)這一篇文章中咱們知道針對序列化的Response有兩個封裝:DataResponse和DownloadResponse。他們都是struct,是純正的存儲設計屬性。和DefaultDataResponse,DefaultDownloadResponse最大的不一樣,其內部多了一個Result的封裝。不明白Result的朋友能夠去看看這篇文章Alamofire源碼解讀系列(五)之結果封裝(Result).閉包

所以只要在上邊的response方法中添加一個參數就行,這個參數的任務就是完成數據的序列化。此時咱們說的系列化就是指能夠把響應數據生成Result的功能。由於DataResponse和DownloadResponse的初始化離不開這個參數。

僞代碼以下:

dataRequest.request().response(queue 序列化者 回調函數)
downloadRequest.request().response(queue 序列化者 回調函數)

咱們之因此把data和download的請求每次都分開來設計,緣由是由於這兩個不一樣的請求獲得的響應不同。download能夠從一個URL中獲取數據,而data不行。

那麼重點來了,序列化者的任務是把數據轉換成Result。所以咱們能夠把這個序列化者設計成一個類或者結構體,裏邊提供一個轉換的方法就好了。這也是最正常不過的思想。可是在swift中咱們應該轉變思惟。swift跟oc不同。

咱們不該該把系列化者用一個固定的對象封死。這個時候就是協議大顯身手的時刻了。既然序列化者須要一個函數,那麼咱們就設計一個包含該函數的協議。這一切的思想應該是從高層到底層的過分的。所以協議就是下邊的代碼:

/// 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等等的需求。在回到序列者的問題上,只要實現了這些協議就行,序列者應該是一個存儲屬性,用序列化函數做爲參數來初始化:

/// 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 {
            /// 這裏就調用了responseSerializer保存的系列化函數,函數調用後會獲得result
            let result = responseSerializer.serializeResponse(
                self.request,
                self.response,
                self.delegate.data,
                self.delegate.error
            )

            /// 這裏必定要記得,DataResponse是一個結構體,是專門爲了純存儲數據的,這裏是調用告終構體的初始化方法建立了一個新的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
    }

擴展

其實,代碼到了這裏,基本的功能已經完成了80%。若是要把data序列成string,只須要建立一個data序列者就行了,可是這樣的設計用起來很麻煩,由於還要書寫序列成Result的函數,這些函數每每都是同樣的,要麼把這些函數提早定義出來,要麼把這些函數封裝起來。

按照Alamofire的設計,是把這些函數封裝起來的。你能夠這麼使用:

dataRequest.request().responseString(queue 回調函數)
dataRequest.request().responseJSON(queue 回調函數)

經過特性的函數來獲取序列化後的response。

responseData

responseData是把數據序列化爲Data類型。也就是Result

生成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函數,那麼這個函數是什麼呢?就是大括號內部的這個閉包,咱們經過下邊的代碼就獲得了一個DataResponseSerializer,這個DataResponseSerializer內部保存着一個函數,函數的做用就是根據參數,最終解析出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)
        }
    }

實現DataRequest的responseData函數:

/// 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 (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)))
            }
        }
    }

實現DataRequest的responseData函數:

/// 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
        )
    }

上邊的代碼中值得注意的是:初始化序列者須要的是一個函數,只要把這個函數看作是一個參數,就能明白爲何會這麼寫。那麼咱們更應該關心的是下邊的函數,它解釋瞭如何根據response: HTTPURLResponse?, data: Data?, error: Error?得到Result。也是序列化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的套路如出一轍,就不把所有的代碼弄過來了,以避免浪費篇幅,咱們應該關心如何根據encoding: String.Encoding?,response: HTTPURLResponse?,data: Data?,error: Error?得到Result

/// 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的套路如出一轍,就不把所有的代碼弄過來了,以避免浪費篇幅,咱們應該關心如何根據options: JSONSerialization.ReadingOptions,response: HTTPURLResponse?,data: Data?,error: Error?得到Result

/// 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多是字典,也多是數組。

responsePropertyList

responsePropertyList跟responseData的套路如出一轍,就不把所有的代碼弄過來了,以避免浪費篇幅,咱們應該關心如何根據options: PropertyListSerialization.ReadOptions,response: HTTPURLResponse?,data: Data?,error: Error?得到Result

/// 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屬性,這是一個計算屬性,所以在不一樣的請求階段會得到不一樣的取值。

總結

因爲知識水平有限,若有錯誤,還望指出

連接

Alamofire源碼解讀系列(一)之概述和使用 簡書-----博客園

Alamofire源碼解讀系列(二)之錯誤處理(AFError) 簡書-----博客園

Alamofire源碼解讀系列(三)之通知處理(Notification) 簡書-----博客園

Alamofire源碼解讀系列(四)之參數編碼(ParameterEncoding) 簡書-----博客園

Alamofire源碼解讀系列(五)之結果封裝(Result) 簡書-----博客園

Alamofire源碼解讀系列(六)之Task代理(TaskDelegate) 簡書-----博客園

Alamofire源碼解讀系列(七)之網絡監控(NetworkReachabilityManager) 簡書-----博客園

Alamofire源碼解讀系列(八)之安全策略(ServerTrustPolicy) 簡書-----博客園

Alamofire源碼解讀系列(九)之響應封裝(Response) 簡書-----博客園

相關文章
相關標籤/搜索