سؤال

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

All posted notifications are sent to observers and handled on the same thread and immediately. So, if gotData: takes time and doesn't push it's processing onto a different thread it will simply be blocking whatever is going to post the next notification. So the next notification will be posted later in time (than you might expect).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top