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

Posted by roysolay on Stack Overflow See other posts from Stack Overflow or by roysolay
Published on 2010-04-26T18:57:05Z Indexed on 2010/04/26 19:03 UTC
Read the original article Hit count: 243

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.

© Stack Overflow or respective owner

Related posts about notification

Related posts about observer