一个关于 NSNotification
的快速问题...如果我在一个方法中发布两个 NSNotifications
,并且它们被不同的对象观察到,那么执行的顺序是什么选择器方法?
例如,如果我有三个 Controller - Poster、Receiver A 和 Receiver B。在 Poster Controller 的函数中,我执行以下操作:
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceiverADoSomething" object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceiverBDoSomething" object:self];
在接收器 A 的 viewDidLoad 方法中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workToDoByA:) name:@"ReceiverADoSomething" object:nil];
在接收器 B 的 viewDidLoad 方法中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workToDoByB:) name:@"ReceiverADoSomething" object:nil];
workToDoByA
会先完成然后是 workToDoByB
吗?
还是一起处决?
另一种情况...如果我有海报 A 发布通知,但同一通知有两个观察员。那么执行顺序是怎样的呢?
预先感谢您的帮助。
最佳答案
摘自 Apple docs :
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.
postNotificationName 的策略相同。
关于objective-c - 发布两个 NSNotifications 时的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5164498/