Search code examples
ioscocoa-touchnsnotificationcenternsnotification

Is postNotificationName guaranteed to dispatch immediately to listeners?


If i do

[[NSNotificationCenter defaultCenter] postNotificationName:@"N" object:self];
NSLog(@"here");

The observer is notified synchronously before NSLog(@"here"); is executed... if all objects are on the same thread, is this guaranteed to be executed synchronously (immediately)?

Cannot find this to be explicitly stated


Solution

  • Yes it is, quite well buried though:

    Using the NSNotificationCenter’s postNotification: method and its variants, you can post a notification to a notification center. However, the invocation of the method is synchronous: before the posting object can resume its thread of execution, it must wait until the notification center dispatches the notification to all observers and returns.

    Source:

    https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Notifications/Articles/NotificationQueues.html