42

If a class registers for NSNotificationCenter events of a certain type and another class posts an event of that type, will the code in the receiver execute before (synchronously) or after (asynchronously) the posting class continues?

- (void)poster {
    [[NSNotificationCenter defaultCenter]
        postNotificationWithName:@"myevent"
        object:nil];
    NSLog(@"Hello from poster");
}

- (void)receiver {
    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector:(mySelector)
        name:@"myevent"
        object:nil];
}

- (void) mySelector:(NSNotification *) notification {
    NSLog(@"Hello from receiver");
}

In the code example above, will "Hello from receiver" be printed before or after "Hello from caller"?

Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
Munna89
  • 613
  • 1
  • 6
  • 10
  • just have a look at http://stackoverflow.com/questions/1900352/what-is-nsnotification – Lochana Ragupathy Apr 30 '13 at 11:30
  • read original post of below answer..http://stackoverflow.com/questions/7880742/ios-are-methods-called-by-delegates-and-observers-executed-on-the-main-thread – BhushanVU Apr 30 '13 at 11:33

1 Answers1

98

As stated in the documentation for NSNotificationCenter NSNotificationCenter Class Reference notifications are posted synchronously.

A notification center delivers notifications to observers synchronously. In other words, the postNotification: methods do not return until all observers have received and processed the notification. To send notifications asynchronously use NSNotificationQueue.

In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

Hope it helps you.

malhal
  • 26,330
  • 7
  • 115
  • 133
Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • 3
    Short and appropriate answer !! – Nipun Arora Jul 24 '14 at 07:16
  • I don't understand the "synchronous" term or your answer is totally incorrect. I wanted to call in the following order: 1)notification; 2)the notification handler; 3)code in the same block in notification. But it is called as 1, 3, 2 - incorrect order – Gargo Aug 27 '14 at 18:50
  • @Gargo if you don't understand the answer that doesn't mean answer is incorrect. This is correct and accepted answer. – Nishant Tyagi Aug 28 '14 at 03:35
  • 1
    I wrote an explanation and it means your phrase "the postNotification: methods do not return until all observers have received and processed the notification" is incorrect. What is wrong with my question about it? – Gargo Aug 28 '14 at 20:39
  • Great answer. Apple's own Notification Programming Topics also answers this explicitly: 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. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/NotificationQueues.html – paperlib Mar 17 '16 at 05:29
  • A simple GitHub project, [Example-Notifications](https://github.com/leanne63/Example-Notifications), demonstrates the *synchronous* nature of notifications in Swift 4, iOS 11. (Also, it appears that notifications aren't posted?/received? during the model's initialization.) – leanne Nov 29 '17 at 17:42
  • To add on this, about _In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself._ That's a good thing, because your thread bound storage is protected all the way through the observer. if the observer needs to do something in a queue, then obviously it can dispatch off to another queue... – mfaani Nov 01 '19 at 19:54
  • @Gargo: Based on the Example-Notifications GitHub project shared above, the call order is 1,2,3 (not 1,3,2), at least now in Jun 2021. – nishanthshanmugham Jun 12 '21 at 01:43