NSDistributedNotifications 未在(相同)应用程序的实例之间分发
在 10.7.2 上,我无法让标准 NSDistributedNotifications 开箱即用。
即使回到(完整的 XCode 版本,位于 https://github.com/dirkx/Example- NSDistribtuedNotification-Failing)到像下面这样简单的东西我得到:
- Splendidlyworking notification 'locals' (Like with a NSNotificationCenter) but
- No inter-app comms当我启动应用程序两次(例如从命令行)时
- ,当其他应用程序为此(或为零)注册时没有通知,
- distnoted 守护程序日志中也没有调试/信息。
我缺少什么?
NSString * kSayNotification = @"org.webweaving.sayExample";
// Send a Distributed Notification on button press.
//
-(IBAction)buttonChange:(NSButton *)sender {
NSString * str = (sender.state == NSOnState) ? @"Yes" : @"No";
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:kSayNotification
object:str
];
}
// Update a label on receiving a Notification.
//
-(void)notif:(NSNotification *)nf {
.. snipped time string ...
// Textfield with the time of arrival and the value passed in the notification.
//
textField.stringValue = [NSString stringWithFormat:@"%@: %@",
dStr, (NSString *)nf.object
];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Register for the notifications.
//
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:@selector(notif:)
name:kSayNotification
object:nil];
}
顺便说一句 - 通知观察程序 (https://github.com/melo/notification-watcher) 不会显示通知 - 但通知会在应用程序内进行处理。
On 10.7.2 I have trouble getting standard NSDistributedNotifications to work out of the box.
Even when brought back to just (Full XCode version at https://github.com/dirkx/Example-NSDistribtuedNotification-Failing) to something as simple as below I get:
- Splendidly working notifications 'locally' (Like with a NSNotificationCenter) but
- No inter-app comms when I start the app up twice (e.g. from the command line)
- No notifications when another apps registers for this (or for nill)
- No debug/info in the distnoted daemon logs either.
What am I missing ?
NSString * kSayNotification = @"org.webweaving.sayExample";
// Send a Distributed Notification on button press.
//
-(IBAction)buttonChange:(NSButton *)sender {
NSString * str = (sender.state == NSOnState) ? @"Yes" : @"No";
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:kSayNotification
object:str
];
}
// Update a label on receiving a Notification.
//
-(void)notif:(NSNotification *)nf {
.. snipped time string ...
// Textfield with the time of arrival and the value passed in the notification.
//
textField.stringValue = [NSString stringWithFormat:@"%@: %@",
dStr, (NSString *)nf.object
];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Register for the notifications.
//
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:@selector(notif:)
name:kSayNotification
object:nil];
}
As an aside - notification watcher (https://github.com/melo/notification-watcher) does not show the Notifiactions - but the notifications are processed within the app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,当没有其他原因导致 CFRunLoop 返回时,消息会无限期地排队。这似乎是设计使然。
我找到了三种解决方法 - 没有一个那么好 - 它们都涉及
deliverImmediately:YES
,deliverImmediately:
和NSNotificationSuspensionBehaviorDeliverImmediately< /code> 给观察者或
显然——这些都不便宜。
干重
Turns out that when there are no other reasons for
CFRunLoop
to return - the messages are queued indefinitely. This seems by design.I found three work arounds for this - none of which is that nice - they both involve
deliverImmediately:YES
to the posting,deliverImmediately:
withNSNotificationSuspensionBehaviorDeliverImmediately
to the observer orObviously - none of this comes cheap.
Dw