取消 NSOperation [英] Cancel NSOperation

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

问题描述

我在循环中使用以下代码:

I am using the following code in a loop:

-(void)getUpdatedComments
{
    if(checkComments)
    {

        objParseOperation=[[ParseOperation alloc] initWithUDID:[[NSUserDefaults standardUserDefaults] valueForKey:kDeviceUDID]:self];
        [operationQueue addOperation:objParseOperation3];

    }
}

其中 operationQueue 是 NSOperationQueue 类型的对象.我每隔几秒钟就调用一次这个方法.

where operationQueue is an object of type NSOperationQueue. I am calling this method every few seconds.

如果我在第一次之后调用这个方法,是需要取消之前的操作还是直接执行[objParseOperation release]?

If I call this method after the first time, do I need to cancel the previous operation or just do [objParseOperation release]?

推荐答案

如果您打算使用同一个队列来添加更多新操作,那么释放它并创建一个新操作并没有多大用处.简单地取消所有操作然后添加更多操作就足够了.您可以通过调用 cancelAllOperations 在队列中.请注意,除非检查取消,否则已经在运行的操作将继续运行.

If you are going to use the same queue for adding more new operations then there isn't much use in releasing it and creating a new one. Simply canceling all operations and then adding more operations is enough. You can do so by calling cancelAllOperations on the queue. Note that operations that are already running will continue to run unless they check for the cancellation.

此方法向当前队列中的所有操作发送取消消息.排队操作在开始执行之前被取消.如果操作已经在执行,则由该操作来识别取消并停止正在执行的操作.

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.

是否释放队列将立即释放排队的操作没有记录,因此应被视为未定义的行为.因此,你不应该假设它会以任何方式工作,你不应该依赖它(参见 危险自动释放 NSOperationQueue).

Wether or not releasing the queue will immediately release the enqueued operations or not is not documented and hence should be considered as undefined behavior. Therefor you shouldn't assume that it will work either way and you should not rely on it (see dangerous to autorelease NSOperationQueue).

然而,有证据表明操作将保留它们的队列,例如 GCD 队列在它们有异步块运行/挂起时由系统保留.您可以阅读 Grand Central Dispatch 文档 表示队列由系统保留,直到块运行完成".尽管如此,文档并没有像上面提到的那样指定 NSOperationQueues 的行为.

There are however evidence that suggests that operations will retain their queue such as that GCD queues are retained by the system while they have asynchronous blocks running/pending. You can read in the Grand Central Dispatch documentation that "the queue is retaind by the system until the block has run to completion". Still, the documentation doesn't specify the behavior for NSOperationQueues like mentioned above.

提交一个块以在调度队列上异步执行并立即返回.

dispatch_async

Submits a block for asynchronous execution on a dispatch queue and returns immediately.

void dispatch_async(
  dispatch_queue_t queue,
  dispatch_block_t block);

参数

排队

提交区块的队列.该队列由系统保留,直到块运行完成.此参数不能为 NULL.

The queue on which to submit the block. The queue is retained by the system until the block has run to completion. This parameter cannot be NULL.

阻止

要提交到目标调度队列的块.此函数代表调用者执行 Block_copy 和 Block_release.此参数不能为 NULL.

The block to submit to the target dispatch queue. This function performs Block_copy and Block_release on behalf of callers. This parameter cannot be NULL.

讨论

这个函数是将块提交到调度队列的基本机制.对这个函数的调用总是在块被提交后立即返回并且永远不会等待块被调用.目标队列确定相对于提交到同一队列的其他块是串行调用还是并发调用该块.独立的串行队列相互并发处理.

This function is the fundamental mechanism for submitting blocks to a dispatch queue. Calls to this function always return immediately after the block has been submitted and never wait for the block to be invoked. The target queue determines whether the block is invoked serially or concurrently with respect to other blocks submitted to that same queue. Independent serial queues are processed concurrently with respect to each other.

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

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