1

Is enumerateUsingBlock: thread safe? I mean, can I mutate an NSMutableArray while enumerating it using enumerateUsingBlock on another thread?

How about enumerateObjectsWithOptions:usingBlock: when using NSEnumerationConcurrent option?

Mirek
  • 470
  • 4
  • 18

1 Answers1

5

No. NSMutableArray is never intrinsically thread safe. All enumeration assumes the collection remains unmodified, including NSEnumerationConcurrent. If you modify the collection during an enumeration, even from the same thread, an exception will be thrown.

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • Thanks. I was thinking that to be the case. I was just confused by the fact that it is possible to enumerate items concurrently. – Mirek Jan 11 '14 at 20:43
  • You'll note that all the enumeration methods are defined on the immutable superclass. There are many cases where you would enumerate an array (serially or concurrently) and not modify it. Mutating an array you're currently enumerating is, at best, a delicate proposition, and IME usually constitutes "bad code smell." – ipmcc Jan 12 '14 at 01:20