iosswiftalamofireafnetworking

How to get current tasks for custom Session in Alamofire 5


Previously I used AFNetworking and could easily get all the tasks for current session by tasks property. I'm trying to migrate to Alamofire 5 and I can't find the same property or way how to get all the tasks. I create the session like this

let session: Session = {
    let configuration = URLSessionConfiguration.af.default
    return Session(configuration: configuration)
}()

I'd like to check does my session contain any active tasks


Solution

  • You don't. If you need to perform actions on all of the currently ongoing Requests, you can use Session.withAllRequests. If all you need to do is cancel all Requests you can use Session.cancelAllRequests. Otherwise you should never access, much less interact with, the URLSessionTasks that underly an Alamofire Session.