Cocoa 基于文档的应用程序:观察者并不总是收到通知

发布于 2024-08-30 16:53:29 字数 1391 浏览 4 评论 0原文

我希望有人可以帮助解决我的通知问题。我有一条通知,看起来设置正确,但未按预期发送。我正在开发一个基于文档的应用程序。委托/文档类在从保存的文件读取时发布通知:

[[NSNotificationCenter defaultCenter] postNotificationName:notifyBsplinePolyOpened object:self];

日志记录告诉我,每当我打开保存的文档时都会到达此行。

在 DrawView 类中,我有 windowOpen 通知和 bsplinePoly 文件打开通知的观察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                                          selector:@selector(mainWindowOpen:)
                                                          name:NSWindowDidBecomeMainNotification
                                                          object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                                          selector:@selector(savedBspline:)
                                                          name:notifyBsplinePolyOpened
                                                          object:nil];

- (void)        mainWindowOpen:(NSNotification*) note
{
        NSLog(@"Window opened");
        _mainWindow = [note object];
}

- (void) savedBspline:(NSNotification*) note
{
        NSLog(@"savedBspline called");
        NSLog(@"note is %@", [note name]);
}

行为很奇怪。当我保存并关闭主窗口并重新打开它时,我收到“窗口已打开”消息,但没有收到“已调用 savedBspline”消息。如果我保持主窗口打开并打开以前保存的会话,我会收到“窗口已打开”消息和“已调用 savedBspline”消息。

我搜索过在线讨论和Apple DevCenter文档,但没有看到这个问题。

I hope somebody can help with my notification problem. I have a notification which looks to be set up correctly but it isn’t delivered as expected. I am developing a document based app. The delegate/ document class posts the notification when it reads from a saved file:

[[NSNotificationCenter defaultCenter] postNotificationName:notifyBsplinePolyOpened object:self];

Logging tells me that this line is reached whenever I open a saved document.

In the DrawView class, I have observers for the windowOpen notification and the bsplinePoly file open notification:

[[NSNotificationCenter defaultCenter] addObserver:self
                                                          selector:@selector(mainWindowOpen:)
                                                          name:NSWindowDidBecomeMainNotification
                                                          object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                                          selector:@selector(savedBspline:)
                                                          name:notifyBsplinePolyOpened
                                                          object:nil];

- (void)        mainWindowOpen:(NSNotification*) note
{
        NSLog(@"Window opened");
        _mainWindow = [note object];
}

- (void) savedBspline:(NSNotification*) note
{
        NSLog(@"savedBspline called");
        NSLog(@"note is %@", [note name]);
}

The behavior is odd. When I save and close the main window and reopen it, I get the “Window opened” message but not the “savedBspline called” message. If I leave a main window open and open a previously saved session, I get the “Window opened” message and the “savedBspline called” message.

I have searched online discussion and Apple DevCenter documentation but I have not seen this problem.

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

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

发布评论

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

评论(1

凉墨 2024-09-06 16:53:29

NSNotification 交付按预期工作 (至少在不涉及 NSNotificationQueue)。它们被立即传递,而不是在其他线程上,没有延迟,并且不被过滤。

我想说,当通知触发时,您的某些对象尚未实例化,或者您以其他方式混淆了执行顺序:您说您在从保存的文件中读取时发布了通知,但随后您错过了保存并关闭窗口时发出通知。

为了帮助调试,我建议您在应用程序委托中设置一个通知观察器,它只记录所有通知。这样您就可以确定所有通知均按预期发送。

NSNotification delivery works as expected (at least when no NSNotificationQueue's are involved). They are delivered immediately, not on other threads, no delay, and not filtered.

I'd say, some of your objects are not yet instantiated when the notification fires or you mixed up the order of execution in some other way: You say that you post the notification when reading from a saved file but then you’re missing the notification while saving and closing the window.

To help debugging I suggest you set up a notification observer in you app delegate which simply logs all notifications. You’re sure then that all notifications are delivered as expected.

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