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

发布于 2025-01-02 09:46:06 字数 1686 浏览 6 评论 0原文

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

即使回到(完整的 XCode 版本,位于 https://github.com/dirkx/Example- NSDistribtuedNotification-Failing)到像下面这样简单的东西我得到:

  1. Splendidlyworking notification 'locals' (Like with a NSNotificationCenter) but
  2. No inter-app comms当我启动应用程序两次(例如从命令行)时
  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) 不会显示通知 - 但通知会在应用程序内进行处理。

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:

  1. Splendidly working notifications 'locally' (Like with a NSNotificationCenter) but
  2. No inter-app comms when I start the app up twice (e.g. from the command line)
  3. No notifications when another apps registers for this (or for nill)
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美的太阳 2025-01-09 09:46:06

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

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

  1. 在发布中添加 deliverImmediately:YES
  2. 以及 deliverImmediately:NSNotificationSuspensionBehaviorDeliverImmediately< /code> 给观察者或
  3. 设置一个计时器来有意地每秒中断 runLoop。

显然——这些都不便宜。

干重

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

  1. adding a deliverImmediately:YES to the posting,
  2. a deliverImmediately: with NSNotificationSuspensionBehaviorDeliverImmediately to the observer or
  3. setting a timer to intentionally interrupt the runLoop every seconds.

Obviously - none of this comes cheap.

Dw

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文