【问题标题】:Is it safe to enumerate through [NSOperationQueue operations]?通过 [NSOperationQueue 操作] 枚举是否安全?
【发布时间】:2011-06-26 00:21:12
【问题描述】:

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

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

由于操作是异步的并且在另一个线程上执行,operations 可以随时更改(包括在主线程执行期间)。快速枚举是否可以防止这种情况,或者我应该copy(和autorelease)操作数组来代替?

【问题讨论】:

    标签: cocoa-touch cocoa nsoperation nsoperationqueue fast-enumeration


    【解决方案1】:

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

    但是:

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

    there 中的“new”一词向我表明,队列将为您完成复制和自动释放,因此您可以安全地枚举数组。

    【讨论】:

    • 确实,非常不错的收获。谢谢彼得。
    猜你喜欢
    • 1970-01-01
    • 2012-08-12
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多