Search code examples
iosnsnotificationcenternsnotification

Lifetime of the NSNotification object


Let's say I have this posting from one class that fires rapidly:

[[NSNotificationCenter defaultCenter] postNotificationName:kGotData object:nil userInfo:someDictionaryObject];

And an observer from another class:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotData:) name:kGotData object:nil];

-(void)gotData:(NSNotification *)notification
{
    NSDictionary *myUserInfo = notification.userInfo;

    // more code to process userInfo, etc.
}

What if gotData take longer time to process myUserInfo than it did receive calls from the notification center?


Solution

  • It can't take longer — calls are entirely synchronous. The notification centre acts as a one-to-many messaging clearing house that decouples actors that might want to listen from actors that might want to announce an event.

    Supposing you had only exactly the one observer, then your code is equivalent to just calling gotData: directly. So no further notification can occur until the previous has finished being processed.