【问题标题】:How to measure response time with Swift Moya?如何使用 Swift Moya 测量响应时间?
【发布时间】:2020-11-16 14:39:52
【问题描述】:

正如我在 Alamofire 上所知道的,我们有时间线对象,我们可以在其中提取响应时间。 但是,如何从 Moya 请求中获取响应时间?

public final class Response: CustomDebugStringConvertible, Equatable {

    /// The status code of the response.
    public let statusCode: Int

    /// The response data.
    public let data: Data

    /// The original URLRequest for the response.
    public let request: URLRequest?

    /// The HTTPURLResponse object.
    public let response: HTTPURLResponse? //this does not return response time
}

【问题讨论】:

    标签: swift rx-swift moya


    【解决方案1】:

    您可以使用 ClosureEventMonitor 自定义 Session。

    class MoyaConfig {
    
        static let monitor: ClosureEventMonitor = {
            let monitor = ClosureEventMonitor()
            monitor.taskDidFinishCollectingMetrics = { _, task, metrics in
                // do something with the metrics (URLSessionTaskMetrics)
            }
            return monitor
        }()
    
        static let apiSession: Session = {
            let configuration = URLSessionConfiguration.default
            let session = Session(configuration: configuration, startRequestsImmediately: false, eventMonitors: [monitor])
            return session
        }()
    }
    let api = MoyaProvider<API>(session: MoyaConfig.apiSession)
    

    https://github.com/Moya/Moya/issues/2073#issuecomment-737126499

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 2015-01-14
      相关资源
      最近更新 更多