当应用程序不在 Lion 上时,NSNotificationCenter Observer 停止接收事件
我有一个应用程序从默认的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道现在回答这个问题有点晚了,仍然是为了我的记录以及如果有人仍在寻找。
我的 OS X 菜单栏应用程序也有同样的问题。我希望该应用程序能够观察所有状态。
原因:
当应用程序失去焦点时,观察者将被暂停。
IE。当应用程序变为非活动状态时,它会调用该方法
,并且默认情况下 NSDistributedNotificationCenter 对象会被挂起。
解决方案:
我为 NSDistributedNotificationCenter 创建了一个对象
,然后当应用程序失去焦点时,调用 applicationDidResignActive 方法并在内部
通过向 setSuspished 方法发送 NO 使 NSDistributedNotificationCenter 对象从挂起状态恢复。
然后应用程序开始观察,即使它失去焦点。
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
and by default the NSDistributedNotificationCenter object gets suspended.
Solution:
I created an object for NSDistributedNotificationCenter
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.
and then app starts observing even when it looses focus.
根据
NSDistributionNotificationCenter
参考https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDistributedNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000396-BCICEHHB< /a>
您可以
使用以下方式将观察者挂起时的行为设置为 NSNotificationSuspensionBehaviorDeliverImmediately,
或者在发布时将
deliverImmediately
设置为 YES,以在挂起状态下立即发送通知。
并确保您不会定期杀死
distnoted
。我忘记了我有一个旧的启动代理脚本来
killall distnoted
以避免内存泄漏。According to
NSDistributionNotificationCenter
referencehttps://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDistributedNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000396-BCICEHHB
You can either
set the observer's behavior when suspended to
NSNotificationSuspensionBehaviorDeliverImmediately
usingor set
deliverImmediately
to YES when postingto 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.看起来将观察者添加到 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