Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel a request Alamofire

I am sending a request which is triggered based on timer. But if I press the back button the request still seems to be active and the response in turns crashes the app. Kindly suggest a way to cancel the request.

Using Xcode 8.2.1 Swift 3

Here is the sample request :

Alamofire.request(path!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: createHeader()).responseJSON {
    response in
    switch response.result {
    case .success(let data):
        success(data as AnyObject?)
    case .failure(let error) :
        failure(error as NSError)
    }
}

Even tried invalidating the timer on viewDidDisappear but to no help.

Thanks !!

like image 868
Ankit Kumar Gupta Avatar asked Jan 05 '17 05:01

Ankit Kumar Gupta


People also ask

How do I cancel a previous request in Swift?

If you want to cancel the request, you need to trace the requests made and try to cancel it. You can store it in an array and cancel every previous request stored. In your code you create a request: let request = Alamofire.


4 Answers

Alamofire 4 / Swift 3 / Xcode 8

You can cancel a single request as below:

1 - First get the request:

let request = Alamofire.SessionManager.default.request(path!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: createHeader()).responseJSON { response in
    switch response.result {
    case .success(let data):
        success(data as AnyObject?)
    case .failure(let error) :
        failure(error as NSError)
    }
}

2 - Then, in your viewDidDisappear, just call:

request.cancel()


You can cancel all requests as below:

Alamofire.SessionManager.default.session.getTasksWithCompletionHandler { (sessionDataTask, uploadData, downloadData) in
    sessionDataTask.forEach { $0.cancel() }
    uploadData.forEach { $0.cancel() }
    downloadData.forEach { $0.cancel() }
}
like image 194
Enrique Avatar answered Oct 23 '22 12:10

Enrique


did you try this solution:

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

i also add a check to verify if is this the request that i want to cancel:

dataTasks.forEach
            {
                if ($0.originalRequest?.url?.absoluteString == url)
                {
                    $0.cancel()
                }
            }
like image 24
Constantin Saulenco Avatar answered Oct 23 '22 12:10

Constantin Saulenco


SWIFT 5

Alamofire.Session.default.session.getTasksWithCompletionHandler({ dataTasks, uploadTasks, downloadTasks in
            dataTasks.forEach { $0.cancel() }
            uploadTasks.forEach { $0.cancel() }
            downloadTasks.forEach { $0.cancel() }
        })
like image 5
Kseniya Avatar answered Oct 23 '22 11:10

Kseniya


How about this:

manager.session.invalidateAndCancel()
like image 2
zero3nna Avatar answered Oct 23 '22 13:10

zero3nna