iOS Development: Using NSNotificationCenter in a threaded environment

NSNotification has long been part of the iOS Development toolset.  Its primary function is to send messages between multiple objects concerning events that have transpired in the code.  NSNotification allows for the passing of data, and can be observed (and responded to) by more than one object.  However, when working in a multi-threaded environment it is important to be careful when writing your responses to observed notifications.

When posting a notification to the NSNotificationCenter, the thread that the developer is on when posting that notification will be the thread to which all of the observers for that notification will respond.  If the iOS Developer is not updating the UI with any of those responses, then the code will run without any issues.

However, if any of those responses do update the User Interface, then developers will start to see intermittent crashes as well as delays in the update of the UI from that response.

Be Aware.  Be Very Aware…

When the iOS Development Professional runs into these types of symptoms, be sure to be aware and on the lookout for someone who might be updating the UI in a thread other than the main thread.  Moreover, if the response needs to update the UI, make sure to jump to the main thread before making that update.  This will save iOS Developers a massive headache should the cause simply be a notification being posted on a different thread and not the main one.


Leave a Comment

Your email address will not be published. Required fields are marked *