xcode - NSDistributedNotifications 未在(相同)应用程序的实例之间分发

标签 xcode macos cocoa nsnotifications nsdistributednotification

在 10.7.2 上,我无法让标准 NSDistributedNotifications 开箱即用。

即使回到(完整的 XCode 版本 https://github.com/dirkx/Example-NSDistribtuedNotification-Failing ),我得到如下简单的内容:

  1. “本地”通知工作出色(如 NSNotificationCenter),但是
  2. 当我启动应用两次(例如从命令行)时,没有应用间通信
  3. 当其他应用为此(或无)注册时不会收到通知
  4. 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) 不会显示通知 - 但通知会在应用内处理。

最佳答案

事实证明,当没有其他原因导致 CFRunLoop 返回时,消息会无限期地排队。这似乎是设计使然。

我找到了三种解决方法 - 没有一个那么好 - 它们都涉及

  1. 在发布中添加 deliverImmediately:YES
  2. a deliverImmediately: with NSNotificationSuspensionBehaviorDeliverImmediately 给观察者或
  3. 设置一个计时器,有意每秒中断一次 runLoop。

显然 - 这些都不便宜。

深度

关于xcode - NSDistributedNotifications 未在(相同)应用程序的实例之间分发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9140305/

相关文章:

ios - NSMutableArray 中的 NSMutableArray 作为单个对象

c++ - qca-ossl 插件未加载。显示错误 : HMAC(SHA1) is not supported

java.lang.RuntimeException : Could not start Selenium session:

python - 如何将交互式 Python 脚本作为 cron 作业运行?

objective-c - 重命名 NSString 常量

xcode 设置——链接映射文件的路径——它是什么?

ios - 贴纸包界面生成器 Storyboard编译器错误组

ios - 如何在 iOS 6 模拟器中自定义运营商名称?

objective-c - 从 NSString 中检索 NSDate

cocoa - 如何使用 Cocoa 绑定(bind)创建可重用的表单?