通过 [NSOperationQueue 操作] 进行枚举是否安全?

发布于 2024-11-17 13:24:26 字数 259 浏览 12 评论 0 原文

通过[NSOperationQueue操作]通过快速枚举进行枚举是否安全?就像这样:

for (NSOperation *op in [operationQueue operations]) {
    // Do something with op
}

由于操作是异步的并且在另一个线程上执行,因此操作可能随时更改(包括在主线程执行期间)。快速枚举是否可以防止这种情况,或者我应该复制(和自动释放)操作数组?

Is it safe to enumerate, via fast enumeration, through [NSOperationQueue operations]? Like so:

for (NSOperation *op in [operationQueue operations]) {
    // Do something with op
}

Since operations are asynchronous and executed on another thread, operations could change at any time (including during the main thread's execution). Does fast enumeration protect against this, or should I copy (and autorelease) the operations array instead?

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

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

发布评论

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

评论(1

享受孤独 2024-11-24 13:24:26

只有当您枚举数组时队列改变了数组,才是安全的。

然而:

返回一个新数组,其中包含队列中当前的操作。

There 向我建议队列将为您执行复制和自动释放,以便您可以安全地枚举数组。

It's only unsafe if the queue mutates the array while you're enumerating it.

However:

Returns a new array containing the operations currently in the queue.

The word “new” in there suggests to me that the queue will do the copy and autorelease for you, so you can safely enumerate through the array.

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