当应用程序不在 Lion 上时,NSNotificationCenter Observer 停止接收事件

发布于 2024-12-15 06:43:21 字数 218 浏览 6 评论 0原文

我有一个应用程序从默认的 NSNotificationCenter 订阅特定类型的通知。

在 OSX Lion 上,它工作正常,只是当应用程序失去焦点(另一个应用程序变为活动状态)时,它会停止接收事件。当应用程序获得焦点时,它会再次开始接收事件。 该应用程序在以前版本的 OSX 上没有这种行为,它总是收到通知,即使它不在焦点上。

我可以做什么来改变这种行为?

谢谢! 内森

I have an app that subscribes to a specific kind of notifications from the default NSNotificationCenter.

On OSX Lion, it works fine, except that when the app loses the focus (another app becomes active), it stops receiving the events. When the app gains the focus, it starts receiving events again.
The app did not have this behavior on previous versions of OSX, it always received notifications, even when it was out of focus.

What can I do to change this behavior?

Thanks!
Nathan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

三生一梦 2024-12-22 06:43:21

我知道现在回答这个问题有点晚了,仍然是为了我的记录以及如果有人仍在寻找。

我的 OS X 菜单栏应用程序也有同样的问题。我希望该应用程序能够观察所有状态。

原因:

当应用程序失去焦点时,观察者将被暂停。

IE。当应用程序变为非活动状态时,它会调用该方法

-(void)applicationDidResignActive:(NSNotification *)notification

,并且默认情况下 NSDistributedNotificationCenter 对象会被挂起。

解决方案:
我为 NSDistributedNotificationCenter 创建了一个对象

NSDistributedNotificationCenter *center=[NSDistributedNotificationCenter defaultCenter];

,然后当应用程序失去焦点时,调用 applicationDidResignActive 方法并在内部
通过向 setSuspished 方法发送 NO 使 NSDistributedNotificationCenter 对象从挂起状态恢复。

-(void)applicationDidResignActive:(NSNotification *)notification
{
    [center setSuspended:NO];
}

然后应用程序开始观察,即使它失去焦点。

I know its a bit late to answer this, still for my records and if some one still searching.

My OS X Menu bar app had the same problem. I wanted the app to observe all states.

Reason:

When the app looses focus,the observer is suspended.

ie. When the application becomes in-active it calls the method

-(void)applicationDidResignActive:(NSNotification *)notification

and by default the NSDistributedNotificationCenter object gets suspended.

Solution:
I created an object for NSDistributedNotificationCenter

NSDistributedNotificationCenter *center=[NSDistributedNotificationCenter defaultCenter];

and then when the app looses focus its call the applicationDidResignActive method and inside
that the NSDistributedNotificationCenter object is made to regain from suspended state by sending NO to setSuspended method.

-(void)applicationDidResignActive:(NSNotification *)notification
{
    [center setSuspended:NO];
}

and then app starts observing even when it looses focus.

凑诗 2024-12-22 06:43:21

根据NSDistributionNotificationCenter参考
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDistributedNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000396-BCICEHHB< /a>

当应用程序不活动时,NSApplication 类会自动暂停分布式通知传递。基于Application Kit框架的应用程序应该让AppKit管理通知发送的暂停。仅基础程序可能偶尔需要使用此方法。

您可以

使用以下方式将观察者挂起时的行为设置为 NSNotificationSuspensionBehaviorDeliverImmediately,

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(NSString *)notificationSender suspensionBehavior:(NSNotificationSuspensionBehavior)suspendedDeliveryBehavior

或者在发布时将 deliverImmediately 设置为 YES,以

- (void)postNotificationName:(NSString *)notificationName object:(NSString *)notificationSender userInfo:(NSDictionary *)userInfo deliverImmediately:(BOOL)deliverImmediately

在挂起状态下立即发送通知。

并确保您不会定期杀死 distnoted
我忘记了我有一个旧的启动代理脚本来 killall distnoted 以避免内存泄漏。

According to NSDistributionNotificationCenter reference
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDistributedNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000396-BCICEHHB

The NSApplication class automatically suspends distributed notification delivery when the application is not active. Applications based on the Application Kit framework should let AppKit manage the suspension of notification delivery. Foundation-only programs may have occasional need to use this method.

You can either

set the observer's behavior when suspended to NSNotificationSuspensionBehaviorDeliverImmediately using

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(NSString *)notificationSender suspensionBehavior:(NSNotificationSuspensionBehavior)suspendedDeliveryBehavior

or set deliverImmediately to YES when posting

- (void)postNotificationName:(NSString *)notificationName object:(NSString *)notificationSender userInfo:(NSDictionary *)userInfo deliverImmediately:(BOOL)deliverImmediately

to send notifications immediately under suspended state.

And make sure you're not periodically killing distnoted.
I forgot I had an old launch agent script to killall distnoted to avoid memory leaks.

恬淡成诗 2024-12-22 06:43:21

看起来将观察者添加到 NSDistributedNotificationsCenter 时的默认行为已更改为 NSNotificationSuspensionBehaviorCoalesce,当应用程序处于非活动状态时不会发送通知。

此处描述:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDistributedNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000396-5567

It looks like the default behavior when adding an observer to a NSDistributedNotificationsCenter has changed to NSNotificationSuspensionBehaviorCoalesce, that does not deliver notifications when the app is inactive.

Described here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDistributedNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000396-5567

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