在单线程中使用 NSNotifications 时是否存在竞争条件问题?这是一个示例方法:
- (void) playerToggled: (NSNotification *) notification {
if (timerPane.playing && ! timerPane.paused) {
[playerPane toggleCurrentPlayer];
[timerPane toggleTimer];
[mainPane playerToggled];
}
}
条件后的前两个调用将触发 mainPane 接收的 NSNotifications。 mainPane 是否保证在这些通知后收到playerToggled 消息?我应该说这段代码似乎按预期工作(playerToggled 总是最后执行)。但我不确定通知存在哪些时间问题,也找不到具体的答案。
最佳答案
没有预期的竞争条件。除了 Dan Donaldson 的回答之外,这里还引用了 NSNotificationCenter 文档中的另一句话:
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.
关于iphone - NSNotification 竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2510704/