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 RSS · Twitter

Leave a Comment