gpt4 book ai didi

ios - 取消请求时 NSOperation 何时从 NSOperationQueue 中删除?

转载 作者:可可西里 更新时间:2023-11-01 05:28:44 24 4
gpt4 key购买 nike

我正在阅读一些文档,解释如何在 NSOperationQueue 中管理 NSOperation。我的重点是,如果用户在进度面板中按下取消按钮或退出应用程序,则始终完全不执行该操作。因此,取消操作以防止它不必要地消耗 CPU 时间。

所以,每当我需要取消操作时,我应该触发 cancel 方法以防止进一步执行。然后我将不得不定期使用操作对象状态 isCancelled 来检查操作是否被取消。以下是我对此的问题:

(1) 在取消请求时,如果 NSOperation 从 NSOperationQueue 中删除,那么我们如何仍然引用该 NSOperation 及其属性 isCancelled

根据 Apple Developer Class Reference:

NSOperationQueue class regulates the execution of a set of NSOperation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task.

(2) 如果我使用的是 ARC,我需要关心取消请求吗?我举个例子。我有 2 个 View Controller A 和 B。在 B 中,我使用 NSOperation 创建 8 到 10 个 NSURLRequest,并将所有请求放入 NSOperationQueue。这里 NSOperationQueue 的对象是 View Controller B 的属性。因此,如果用户按下后退按钮返回 View A,在 ARC 下,NSOperationQueue 的对象应该自动删除(当我弹出到 View A 时)。 ARC 机制是否会取消所有操作,或者我仍然应该有一些机制来避免无用的执行?

最佳答案

首先,只有当操作的 isFinished 属性变为真时,操作才会从队列中移除。

其次,如果你释放B,那么队列将被释放,操作也会被释放。但是您应该确保在您的代码中您没有在稍后的时间点引用这些操作对象或队列。

关于ios - 取消请求时 NSOperation 何时从 NSOperationQueue 中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25883679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com