美文网首页
Alamofire(4)

Alamofire(4)

作者: 忆痕无殇 | 来源:发表于2019-08-23 16:00 被阅读0次

    1:timeline时间轴

    1)Alamofire的时间轴记录请求从生到死的时间节点信息,(时光🐔🐔🐔🐔🐔)。

     public init(
            requestStartTime: CFAbsoluteTime = 0.0, 
            initialResponseTime: CFAbsoluteTime = 0.0,
            requestCompletedTime: CFAbsoluteTime = 0.0,
            serializationCompletedTime: CFAbsoluteTime = 0.0)
        {
            self.requestStartTime = requestStartTime  //初始化请求时间
            self.initialResponseTime = initialResponseTime // 从服务器接收或发送第一个字节的时间
            self.requestCompletedTime = requestCompletedTime // 请求完成时间
            self.serializationCompletedTime = serializationCompletedTime // 响应序列化完成时间
    
            self.latency = initialResponseTime - requestStartTime //从请求开始到服务器的初始响应的时间间隔(以秒为单位)
            self.requestDuration = requestCompletedTime - requestStartTime//从请求开始到请求完成的时间间隔(以秒为单位)
            self.serializationDuration = serializationCompletedTime - requestCompletedTime // 从请求完成到响应序列化完成的时间间隔
            self.totalDuration = serializationCompletedTime - requestStartTime // 从请求开始到完成时间响应序列化的时间间隔
        }
    

    2)应用
    Request的扩展

    extension Request {
        var timeline: Timeline {
            let requestStartTime = self.startTime ?? CFAbsoluteTimeGetCurrent()
            let requestCompletedTime = self.endTime ?? CFAbsoluteTimeGetCurrent()
            let initialResponseTime = self.delegate.initialResponseTime ?? requestCompletedTime
    
            return Timeline(
                requestStartTime: requestStartTime,   //调用resume的时候开启
                initialResponseTime: initialResponseTime, //queue.addOperation 控制
                requestCompletedTime: requestCompletedTime, // timeline() 
                serializationCompletedTime: CFAbsoluteTimeGetCurrent() // response回调发起的时间
            )
        }
    }
    
    

    response 调用

            timeline: Timeline = Timeline(),
    

    2:adapter适配器和retrier重连

    RequestAdapter 请求适配器,目的是自定义修改请求,一个典型的例子是为每一个请求调价Token请求头
    RequestRetrier 请求重试器, 目的是控制请求的重试机制,一个典型的例子是当某个特殊的请求失败后,是否重试。
    遵守RequestAdapter、RequestRetrier协议,内容需要自己实现

    public protocol RequestAdapter {
        func adapt(_ urlRequest: URLRequest) throws -> URLRequest
    }
    
    public protocol RequestRetrier {
        func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion)
    }
    
    

    具体使用

    class LGAdapter: RequestAdapter,RequestRetrier{
        func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
            print("manager = \(manager)")
            print("request = \(request)")
            print("error = \(error)")
            completion(true,1)
            
            completion(false,0)
        }
        
        func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
            // token
            // 1: request 处理
            // 2: request 重定向
            var request = urlRequest
            request.setValue("lgcoociToken", forHTTPHeaderField: "lgtoken")
            let newUrlRequest = URLRequest.init(url: URL(string: "http://www.douban.com/j/app/radio/channels")!)
            return newUrlRequest
        }
    }
    

    在使用的时候直接调用。

    3:request

    1)DataRequest
    2)DownloadRequest

    public enum Result<Value> {
        case success(Value)
        case failure(Error)
    
        /// Returns `true` if the result is a success, `false` otherwise.
        public var isSuccess: Bool {
            switch self {
            case .success:
                return true
            case .failure:
                return false
            }
        }
    
        /// Returns `true` if the result is a failure, `false` otherwise.
        public var isFailure: Bool {
            return !isSuccess
        }
    
        /// Returns the associated value if the result is a success, `nil` otherwise.
        public var value: Value? {
            switch self {
            case .success(let value):
                return value
            case .failure:
                return nil
            }
        }
    
        /// Returns the associated error value if the result is a failure, `nil` otherwise.
        public var error: Error? {
            switch self {
            case .success:
                return nil
            case .failure(let error):
                return error
            }
        }
    }
    

    //标示请求成功或者失败,成功有成功Value失败有失败的Error。

    4:response

    1)DataResponse


    dataResponse.jpg

    2)DefaultDownloadResponse
    3)DownloadResponse
    4)DefaultDataResponse

    相关文章

      网友评论

          本文标题:Alamofire(4)

          本文链接:https://www.haomeiwen.com/subject/iehosctx.html