NSNotificationCenter事件是同步接收还是asynchronous接收?

如果某个类注册了某个types的NSNotificationCenter事件,并且另一个类发布了该types的事件,则接收器中的代码将在发布类继续之前(同步)还是之后(asynchronous)执行?

 - (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"); } 

在上面的代码示例中,将“来自呼叫者的Hello”之前或之后打印“来自接收者的Hello”?

如NSNotificationCenter NSNotificationCenter类参考文档中所述,同步发布通知。

通知中心同步向观察员发送通知。 换句话说,postNotification:方法在所有观察者都收到并处理通知之前不会返回。 要asynchronous发送通知,请使用NSNotificationQueue

在multithreading应用程序中,通知总是在发布通知的线程中传递,而这些线程可能与观察者自己注册的线程不同。

希望它可以帮助你。