简体   繁体   中英

How to call custom method right before sessionTask resume in Alamofire

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM