如何立即强制取消与AFNetworking的NSOperation? [英] How to immediately force cancel an NSOperation with AFNetworking?

查看:216
本文介绍了如何立即强制取消与AFNetworking的NSOperation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用AFNetworking,我正在尝试在我的进度HUD中实现点击取消功能。我有一个单例类管理所有的HTTP请求。如果进度HUD被点击,我调用:

I am using AFNetworking in my application and I am attempting to implement a "Tap To Cancel" feature in my progress HUD. I have a singleton class that manages all of the HTTP requests. If the progress HUD is tapped, I call:

[[[HTTPRequestSingleton sharedClient] operationQueue] cancelAllOperations];

但这不会像我需要的那样取消操作。我阅读了 NSOperationQueue docs ,并遇到了以下问题:

But this doesn't "cancel" the operation like I need it to. I read up on the NSOperationQueue docs and came across this:


取消操作对象队列,但通知对象它应该尽快中止其任务。对于当前执行的操作,这意味着操作对象的工作代码必须检查取消状态,停止它正在做什么,并将自己标记为完成。对于已排队但尚未执行的操作,队列必须仍然调用操作对象的start方法,以便它可以处理取消事件并将其标记为已完成。

Canceling an operation object leaves the object in the queue but notifies the object that it should abort its task as quickly as possible. For currently executing operations, this means that the operation object’s work code must check the cancellation state, stop what it is doing, and mark itself as finished. For operations that are queued but not yet executing, the queue must still call the operation object’s start method so that it can processes the cancellation event and mark itself as finished.

关于 cancelAllOperations 方法:


向当前队列中的所有操作发送取消消息。队列操作在开始执行之前被取消。

This method sends a cancel message to all operations currently in the queue. Queued operations are cancelled before they begin executing. If an operation is already executing, it is up to that operation to recognize the cancellation and stop what it is doing.

我的问题似乎已经解决了,如果一个操作已在执行,具体涉及已经执行的操作,我想立即取消。使用AFNetworking时,如何提醒操作应该取消并舍弃该请求的所有信息?

My problem seems to specifically involve an operation that is already executing, which I want to immediately cancel. With AFNetworking, how can I alert the operation that it should cancel and discard all information about the request?

AFJSONRequestOperation *loginOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

    //operation was successful

    if (loginOperation.isCancelled)
    {
        //can't do this. variable 'loginOperation' is uninitialized when captured by block      
    }

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

    //operation failed

}];


推荐答案

过了一整个上午通过AFNetworking源代码,事实证明,我的操作没有取消的原因与操作本身无关,而是因为我一直在开始操作不正确的所有这一次。我一直在使用

After a full morning of digging through AFNetworking source code, it turns out that the reason my operations weren't canceling had nothing to do with the operations themselves, but rather because I've been starting the operations incorrectly all this time. I've been using

[NSOperation start];

当我应该将它添加到我的 HTTPRequestSingleton 的操作队列:

when I should have been adding it to my HTTPRequestSingleton's operation queue:

[[[HTTPRequestSingleton sharedClient] operationQueue] addOperation:NSOperation];

将其添加到队列可以正确取消它,而不必检查 isCancelled 属性。

Adding it to the queue allows it to be canceled properly without having to check the isCancelled property.

这篇关于如何立即强制取消与AFNetworking的NSOperation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆