2012-02-04 168 views
2

10.7.2我無法獲得標準NSDistributedNotifications以開箱即用。NSDistributedNotifications未在(相同)應用程序的實例之間分佈

即使(在https://github.com/dirkx/Example-NSDistribtuedNotification-Failing完整的XCode版)帶回只是下面我簡單的東西得到:

  1. 漂漂工作的通知「本地」(就像用NSNotificationCenter),但
  2. 沒有間-app通信當我啓動應用程序兩次(例如,從命令行)
  3. 當另一個應用程序註冊此(或n)時沒有通知
  4. Distnote守護進程日誌中沒有調試/信息。

我錯過了什麼?

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)不顯示Notifiactions - 但通知在應用內處理。

+0

註冊通知的代碼是什麼樣的? – sbooth 2012-02-04 12:37:20

+0

這是 - (void)applicationDidFinishLaunching中的函數:(NSNotification *)在上面的aNotification(或查看實際git的鏈接https://github.com/dirkx/Example-NSDistribtuedNotification-Failing/blob/master/SayHi /SayHi/AppDelegate.m - 第56行及以後)。 – 2012-02-04 13:32:40

+0

謝謝,我想我早些時候忽略了這一點。 – sbooth 2012-02-04 14:30:48

回答

0

原來,當CFRunLoop沒有其他原因返回時 - 消息無限期排隊。這似乎是設計。

我發現了三個變通本 - 其中沒有一個是好的 - 它們都涉及到

  1. 添加deliverImmediately:YES向投稿,
  2. 一個deliverImmediately:NSNotificationSuspensionBehaviorDeliverImmediately觀察者或
  3. 設置每秒鐘有意中斷runLoop的定時器。

顯然 - 這些都不便宜。

Dw

相關問題