Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to enumerate through [NSOperationQueue operations]?

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?

like image 812
Adam Ernst Avatar asked Jun 26 '11 00:06

Adam Ernst


1 Answers

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.

like image 110
Peter Hosey Avatar answered Dec 30 '22 10:12

Peter Hosey