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) 简书-----博客园

posted @ 2017-03-29 16:05  马在路上  阅读(1729)  评论(0编辑  收藏