【问题标题】:How to call custom method right before sessionTask resume in Alamofire如何在 Alamofire 中的 sessionTask 恢复之前调用自定义方法
【发布时间】:2020-07-03 03:45:14
【问题描述】:

当 Alamofire 调用 resume 以启动请求时,我需要记录一些数据/信息。 (我在项目中使用了 Swift)

还有没有domethod_swizzling

时间线会是这样的:

Call a request (put in request queue or execute right away) -> [custom method] -> SessionTask.resume()

我知道 Moya 做过类似的事情,叫做WillSend。但我想知道如何在不使用 Moya 的情况下做到这一点。

谢谢。

【问题讨论】:

  • 那时你打算做什么?
  • @JonShier 我想获取请求开始时间的信息或它发送的请求的参数或与即将执行的请求相关的任何信息。你有什么建议吗?

标签: swift httprequest alamofire nsurlsessiontask


【解决方案1】:

如果您只需要检查各种请求元素,您可以使用 Alamofire 5 的 EventMonitor 协议,该协议在 Alamofire 发出请求时在各种生命周期事件中调用。有一个内置的 ClosureEventMonitor 允许您为这些事件设置闭包。例如:

let monitor = ClosureEventMonitor()
monitor.requestDidCompleteTaskWithError = { (request, task, error) in
    debugPrint(request)
}
let session = Session(eventMonitors: [monitor])

请参阅our documentation for ClosureEventMonitorEventMonitor protocol itself

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2015-03-02
    • 2017-11-23
    相关资源
    最近更新 更多