繁体   English   中英

NSDistributedNotifications不分布在(相同)应用程序的实例之间

[英]NSDistributedNotifications not distributed between instances of (same) app(s)

在10.7.2上,我很难使标准的NSDistributedNotifications开箱即用。

即使回到刚才的简单代码( https://github.com/dirkx/Example-NSDistribtuedNotification-Failing的完整XCode版本),我也会得到如下所示的简单信息:

  1. 出色的“本地”工作通知(例如NSNotificationCenter),但是
  2. 当我两次启动应用程序时(例如,从命令行),没有应用程序间通信
  3. 当其他应用程序为此注册(或无效)时没有通知
  4. 分布式守护进程日志中也没有调试/信息。

我想念什么?

 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)不显示通知-但通知在应用程序处理的。

事实证明,如果没有其他原因让CFRunLoop返回,则消息将无限期排队。 这似乎是设计使然。

我为此找到了三种解决方法-都不是那么好-它们都涉及

  1. 在发布中添加一个deliverImmediately:YES
  2. deliverImmediately:使用NSNotificationSuspensionBehaviorDeliverImmediately立即deliverImmediately:给观察者或
  3. 设置计时器以故意每秒钟中断一次runLoop。

显然-这些都不便宜。

w

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM