xcode 取消请求 Alamofire

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41478122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:42:25  来源:igfitidea点击:

Cancel a request Alamofire

iosxcodeswift3http-postalamofire

提问by Ankit Kumar Gupta

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

使用 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.

甚至尝试使 viewDidDisappear 上的计时器无效,但无济于事。

Thanks !!

谢谢 !!

回答by Enrique

Alamofire 4 / Swift 3 / Xcode 8

阿拉莫菲尔 4 / Swift 3 / Xcode 8

You can cancel a single requestas below:

您可以取消单个请求,如下所示:

1 - First get the request:

1 - 首先获取请求:

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:

2 - 然后,在您的viewDidDisappear 中,只需调用:

request.cancel()

request.cancel()



You can cancel all requestsas below:

您可以取消所有请求,如下所示:

Alamofire.SessionManager.default.session.getTasksWithCompletionHandler { (sessionDataTask, uploadData, downloadData) in
    sessionDataTask.forEach { 
let sessionManager = Alamofire.SessionManager.default 
sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in 
dataTasks.forEach { 
dataTasks.forEach
            {
                if (
manager.session.invalidateAndCancel()
.originalRequest?.url?.absoluteString == url) { ##代码##.cancel() } }
.cancel() } uploadTasks.forEach { ##代码##.cancel() } downloadTasks.forEach { ##代码##.cancel() } }
.cancel() } uploadData.forEach { ##代码##.cancel() } downloadData.forEach { ##代码##.cancel() } }

回答by Constantin Saulenco

did you try thissolution:

您是否尝试过解决方案:

##代码##

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

我还添加了一个检查来验证这是否是我要取消的请求:

##代码##

回答by zero3nna

How about this:

这个怎么样:

##代码##

回答by Cody

In case it helps anyone, in my case I had a RequestRetrier on the SessionManager that retried the request after it was canceled, so be sure to check there!

如果它对任何人有帮助,就我而言,我在 SessionManager 上有一个 RequestRetrier,它在取消后重试请求,所以一定要检查那里!