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


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

前言

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

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

不序列化的設計

我們先從最簡單的事情着手。如果我發起了一個請求,我肯定希望知道請求的結果,那么就會有下邊這樣的偽代碼:

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) 簡書-----博客園


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM