取消NS操作

发布于 2025-01-04 15:03:41 字数 450 浏览 1 评论 0原文

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

-(void)getUpdatedComments
{
    if(checkComments)
    {

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

    }
}

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

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

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];

    }
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你好,陌生人 2025-01-11 15:03:41

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

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

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

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

dispatch_async

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

voiddispatch_async(
  dispatch_queue_t 队列,
  dispatch_block_t 块);

参数

队列

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

阻止

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

讨论

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

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.

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).

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);

Parameters

queue

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

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.

Discussion

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文