Tuesday, April 22, 2014

NSNotificationCenter Is Not Thread-safe

Jeff Johnson:

In fact, if your app has multiple threads, then you’re almost certainly using NSNotificationCenter wrong.

[…]

The above code crashes reliably in the NSLog after sleep. Why? What we see here is that removeObserver: does not block until all notifications have been posted. The method can return while a notification is still executing on another thread. Thus, we have a race condition.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment