开发者

Cocoa document-based app: Notification not always received by observer

开发者 https://www.devze.com 2022-12-28 10:22 出处:网络
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/ do

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];
[[NSNotificationCent开发者_开发问答er 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消