簡體   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