[英]NSDistributedNotifications not distributed between instances of (same) app(s)
在10.7.2上,我很難使標准的NSDistributedNotifications開箱即用。
即使回到剛才的簡單代碼( https://github.com/dirkx/Example-NSDistribtuedNotification-Failing的完整XCode版本),我也會得到如下所示的簡單信息:
我想念什么?
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
返回,則消息將無限期排隊。 這似乎是設計使然。
我為此找到了三種解決方法-都不是那么好-它們都涉及
deliverImmediately:YES
, deliverImmediately:
使用NSNotificationSuspensionBehaviorDeliverImmediately
立即deliverImmediately:
給觀察者或 顯然-這些都不便宜。
w
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.