Alamofire: Cancel all request

Created on 18 Oct 2016  路  8Comments  路  Source: Alamofire/Alamofire

Hi,

I recently migrating my code to Swift 3 and Alamofire 4. Before I use two different code to cancel all requests :
Alamofire.manager.session.getAllTasksWithCompletionHandler { (tasks) -> Void in tasks.forEach({ $0.cancel() }) }
or
Alamofire.manager.session.invalidateAndCancel()

But in Alamofire 4, I can't find a way to cancel all requests. Do you have an idea ?

Most helpful comment

let sessionManager = Alamofire.SessionManager.default sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in dataTasks.forEach { $0.cancel() } uploadTasks.forEach { $0.cancel() } downloadTasks.forEach { $0.cancel() } }

We use this code to cancel all requests (normal / upload / download) With Alamofire 4

All 8 comments

let sessionManager = Alamofire.SessionManager.default sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in dataTasks.forEach { $0.cancel() } uploadTasks.forEach { $0.cancel() } downloadTasks.forEach { $0.cancel() } }

We use this code to cancel all requests (normal / upload / download) With Alamofire 4

Thanks a lot ;)

Thanks @jookes

Thanks @jookes

I am pissed of this error past 2 year in Swift ,whenever I hit the API Continuously
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x608000458270 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=http://citeall.com/devapp17/api/v1.0/app/get/getLoyality, NSErrorFailingURLKey=http://citeall.com/devapp17/api/v1.0/app/get/getLoyality, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.

Please help me to overcome this problem

Alamofire.SessionManager.default.session.getAllTasks { (tasks) in

    tasks.forEach({$0.cancel()})
}

Please use this code for Cancel all request in swift 4.

Why do errors appear in console after cancelling tasks?

@jookes is there any call back option after cancelling all the requests. I need to perform certain task after all these calls get cancelled

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tobiasoleary picture tobiasoleary  路  3Comments

matthijsotterloo picture matthijsotterloo  路  3Comments

dpstart picture dpstart  路  3Comments

borek2 picture borek2  路  3Comments

solomon23 picture solomon23  路  3Comments