I need to record some data/info when Alamofire called resume to start the request. (I used Swift in my project)
Is there anyway without do method_swizzling
?
the timeline will be like this:
Call a request (put in request queue or execute right away) -> [custom method] -> SessionTask.resume()
I know Moya did something similar called WillSend
. But I would like to know how to do it without using Moya.
Thank you.
If all you need to do is inspect various request elements you can use Alamofire 5's EventMonitor
protocol, which is called during various lifetime events as Alamofire makes requests. There's a built in ClosureEventMonitor
which allows you to set closures for those events. For example:
let monitor = ClosureEventMonitor()
monitor.requestDidCompleteTaskWithError = { (request, task, error) in
debugPrint(request)
}
let session = Session(eventMonitors: [monitor])
Please see our documentation for ClosureEventMonitor
and the EventMonitor
protocol itself.