Dan*_*son 2 iphone cocoa race-condition nsnotifications
在单个线程中使用NSNotifications时是否存在任何竞争条件问题?这是一个示例方法:
- (void) playerToggled: (NSNotification *) notification {
if (timerPane.playing && ! timerPane.paused) {
[playerPane toggleCurrentPlayer];
[timerPane toggleTimer];
[mainPane playerToggled];
}
Run Code Online (Sandbox Code Playgroud)
}
条件之后的前两个调用将触发mainPane将接收的NSNotifications.mainPane是否保证在这些通知之后收到playerToggled消息?我应该说这个代码似乎按照需要工作(playerToggled总是执行最后).但我不确定通知的时间问题是什么,我找不到具体的答案.
没有预期的竞争条件.除了Dan Donaldson的回答,这里还有来自NSNotificationCenter文档的另一个引用:
通知中心同步向观察者发送通知.换句话说,postNotification:方法在所有观察者都收到并处理通知之前不会返回.要异步发送通知,请使用NSNotificationQueue.
| 归档时间: |
|
| 查看次数: |
604 次 |
| 最近记录: |