views:

93

answers:

1

Hi, 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.

+1  A: 

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.

Nikolai Ruhe
Thank you Nikolai. I spent hours staring at my code w/o success and your comments guided me straight to the problem. The observer is called out in the DrawView class's initWithFrame method. Using logging, I found out that the "missing" notification is sent before this method is called when a new window is opened. I will modify the code to addObserver where it will pick up the notification.
roysolay