Is there any way I can for example say:
Alamofire.Manager.cancelAllRequests()
or Alamofire.Manager.sharedInstance.cancelAllRequests()
?
You should use the NSURLSession
methods directly to accomplish this.
Alamofire.SessionManager.default.session.invalidateAndCancel()
This will call all your completion handlers with cancellation errors. If you need to be able to resume downloads, then you'll need to grab the resumeData
from the request if it is available. Then use the resume data to resume the request in place when you're ready.
in completion to the @Loïs Di Qual you can check the request url and cancel (suspend, resume) the request that you need:
downloadTasks.forEach
{
if ($0.originalRequest?.url?.absoluteString == url)
{
$0.cancel()
}
}
class func StopAPICALL() {
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
dataTasks.forEach { $0.cancel() }
uploadTasks.forEach { $0.cancel() }
downloadTasks.forEach { $0.cancel() }
}
}
cnoon
's one-liner solution is great but it invalidates the NSURLSession and you need to create a new one.
Another solution would be this (iOS 7+):
session.getTasks { dataTasks, uploadTasks, downloadTasks in
dataTasks.forEach { $0.cancel() }
uploadTasks.forEach { $0.cancel() }
downloadTasks.forEach { $0.cancel() }
}
Or if you target iOS 9+ only:
session.getAllTasks { tasks in
tasks.forEach { $0.cancel() }
}
If it helps, I got cnoon's answer to work on my own instance of an Alamofire.Manager
. I have a singleton class called NetworkHelper which has a property called alamoFireManager, which handles all my network requests. I just call the NSURSession
invalidateAndCancel()
on that alamoFireManager
property, reset my manager in setAFconfig()
, then I'm good to go.
class NetworkHelper {
private var alamoFireManager : Alamofire.Manager!
class var sharedInstance: NetworkHelper {
struct Static {
static var instance: NetworkHelper?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = NetworkHelper()
}
return Static.instance!
}
init(){
setAFconfig()
}
func setAFconfig(){
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForResource = 4
configuration.timeoutIntervalForRequest = 4
alamoFireManager = Alamofire.Manager(configuration: configuration)
}
func cancelAllRequests() {
print("cancelling NetworkHelper requests")
alamoFireManager.session.invalidateAndCancel()
setAFconfig()
}
Below Code stops the Requests in [Swift 3]:
Plus the code works for Alamofire v3 & v4 plus for iOS 8+.
func stopTheDamnRequests(){
if #available(iOS 9.0, *) {
Alamofire.SessionManager.default.session.getAllTasks { (tasks) in
tasks.forEach{ $0.cancel() }
}
} else {
Alamofire.SessionManager.default.session.getTasksWithCompletionHandler { (sessionDataTask, uploadData, downloadData) in
sessionDataTask.forEach { $0.cancel() }
uploadData.forEach { $0.cancel() }
downloadData.forEach { $0.cancel() }
}
}
}
Simply Copy and paste the function.