Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
通过快速枚举来枚举是否安全[NSOperationQueue operations]?像这样:
[NSOperationQueue operations]
for (NSOperation *op in [operationQueue operations]) { // Do something with op }
由于操作是异步的并在另一个线程上执行,operations因此可能随时更改(包括在主线程执行期间)。快速枚举是否可以防止这种情况,或者我应该copy(和autorelease)操作数组来代替?
operations
copy
autorelease
如果队列在您枚举它时改变了数组,这只是不安全的。
然而:
返回一个包含队列中当前操作的新数组。
那里的“新”一词向我暗示,队列将为您执行复制和自动释放,因此您可以安全地枚举数组。