gpt4 book ai didi

ios - 如果我调用 cancelAllOpeartions,OperationQueue 不会从队列中删除操作

转载 作者:行者123 更新时间:2023-11-28 15:45:54 25 4
gpt4 key购买 nike

我有一个操作队列,我正在调用 cancelAllOpeations 但如果我问OperationQueue.operationcount 它不会让我返回零。

我正在覆盖取消方法,一切正常,但 opertioncount 不为零。这是预期的吗?

最佳答案

参见 Apple's API Document对于 NSOperation cancel 方法(强调我的):

This method does not force your operation code to stop. Instead, it updates the object’s internal flags to reflect the change in state. If the operation has already finished executing, this method has no effect. Canceling an operation that is currently in an operation queue, but not yet executing, makes it possible to remove the operation from the queue sooner than usual.

如果操作在队列中,cancel 方法会将操作标记为“就绪”,如果不在队列中,则将其标记为立即完成。由于您的操作在队列中,这意味着取消的操作将“更快”开始。如果子类正确,您取消的操作应立即将其自身标记为已完成并生成其最终 KVO 通知。只有这样,您的操作才会出队。

另见 Responding to the Cancel Command有关取消自定义操作的更多信息。

如果您需要知道操作队列何时在其 operations 数组属性中剩余 0 个操作,您可以考虑将队列的所有者注册为 operationCount 键的观察者使用 KVO 的路径。然后,当您收到该属性的值已更改的通知时,您可以检查该值是否为 0,然后执行所需的任何逻辑。请注意,NSOperations 在它们正在操作的线程上发送它们的 KVO 通知,如果它们从 NSOperationQueue 运行,通常将成为后台线程——这意味着如果您需要执行任何 UI/阻塞逻辑,您将需要确保它在主线程上运行。

如果您决定使用 KVO 添加观察者,请确保通过稍后删除观察者来平衡它。事实上,如果您决定利用 KVO,我强烈建议您消化所有 KVO programming guide并通读 KVO API docs ,在使用该框架时任何缺乏尽职调查的行为都可能导致未定义的行为、内存泄漏,甚至 bad_access 崩溃。

关于ios - 如果我调用 cancelAllOpeartions,OperationQueue 不会从队列中删除操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43070143/

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