NSNotificationCenter Observer Not Receiving Notification
Asked Answered
I

3

8

I am aware of the standard reasons as to why a notification would not be received:

  • Deallocating or nullifying the registered object.
  • Removing the object as an observer.
  • Not registering as an observer.
  • Registering for the wrong notification or posting the wrong notification.

I can happily say that I am damn sure that none of these are happening. I suppose the most likely is that the object is at some point being nullified and recreated, but it registers for the notification upon initialisation.

Here is where I register:

/**
 *  initialises an object with a unique file url
 *
 *  @param  url                         the url to set as the file url
 */
- (id)initWithFileURL:(NSURL *)url
{
    if (self = [super initWithFileURL:url])
    {
        self.entries                    = [[NSMutableArray alloc] init];

        //  we want to be notified when a note has changed
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(noteChanged)
                                                     name:@"com.andbeyond.jamesvalaitis.notesChanged"
                                                   object:self];

        NSLog(@"Just registered for 'com.andbeyond.jamesvalaitis.notesChanged'");
    }

    return self;
}

Here is where I post the notification:

/**
 *  save the note content
 */
- (void)saveNote
{
    if (_isChanged)
    {
        //  save to the text view to the note's contents
        self.note.noteContent           = self.noteView.text;

        //  post a notification that we changed the notes
        [[NSNotificationCenter defaultCenter] postNotificationName:@"com.andbeyond.jamesvalaitis.notesChanged" object:nil];

        NSLog(@"Just posted 'com.andbeyond.jamesvalaitis.notesChanged'");

        //  make sure we know it's already saved
        _isChanged                          = NO;
    }
}

This is the method that is not being called:

/**
 *  called when a note has changed
 */
- (void)noteChanged:(NSNotification *)notification
{
    NSLog(@"Just received for 'com.andbeyond.jamesvalaitis.notesChanged'");

    //  save the notes
    [self saveToURL:self.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success)
    {
        if (success)
            NSLog(@"Note updated.");
    }];
}

This is the console clarifying that I both register and post the notification:

2012-11-15 13:27:50.958 iCloud Custom[11269:907] Just registered for 'com.andbeyond.jamesvalaitis.notesChanged'

2012-11-15 13:28:24.184 iCloud Custom[11269:907] Just posted 'com.andbeyond.jamesvalaitis.notesChanged'

The whole project can be found here.

Induce answered 15/11, 2012 at 13:38 Comment(4)
Add the noteChanged method to the post...Irritate
your selector is still like this @selector(noteChanged) .. then how will it call this - (void)noteChanged:(NSNotification *)notification ??? you need to change your selector like this @selector(noteChanged:)Spireme
@DineshRaja: Hi Dinesh and James, I am facing a similar issue and having been through all the answers on this post, applying the suggestions; still things haven't worked out for me. I am able to post the Notification successfully and declare the Observer as well. But the logic where the check is made doesn't gets executed. How should I share the code? Or should I post a rather duplicate question?Huntingdonshire
@ArchitKapoor You can post a new question with your code or put a code on gist/pastebin and share link here. We could help you then.Spireme
S
23

I think I have figured out the answer. You are creating the notification in UIDocument file named NotesDocument.m . So when you create an observer, you set the object as self. That means the NotesDocument object . But when you post the notification, you are sending the object as nil. So it wont observe the notification as per documentation. Easy way to overcome this is you need set object as nil when creating a notification. Otherwise you need to pass an NotesDocument Object.

Check out the below image and parameter details for the addObserver method of notification.

enter image description here

check out the notificationSender parameter .The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.

Spireme answered 15/11, 2012 at 14:16 Comment(4)
What an idiot I have been. I totally didn't mean to set the object as self when I registered for the notification. This works perfectly now. Thank you so damn much.Induce
I just had the exact same issue, and can't believe I found the answer so quickly. This is such a tiny thing; so easy to overlook and so difficult to spot. Thank you very much.Rosemaria
About 20 minutes spent thinking I had nil as the object ini the observer, and it was self. Thank you for this post to trigger me to check what I expected the pink-colored property to be correct. :)Azov
Had the same thing in Swift 3, though my case was mistakenly defining notification name when adding an observer. Your answer helped =)Derange
I
0

Change the following:

...
selector:@selector(noteChanged:) 

...
- (void) noteChanged:(NSNotification *) notification
...
Irritate answered 15/11, 2012 at 13:45 Comment(2)
Just made this change, and it did not work. Thank you anyway.Induce
Try using a static string in both places instead of a different string for the notification. They appear to be exactly the same, but there could be something odd.Irritate
U
-6

The following url will solve your problem.

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_nsnotificationcenter

Underpin answered 15/11, 2012 at 13:44 Comment(1)
I have used NSNotificationCenter a lot and am aware of the process of implementing it. You adamance that your link would help me is quite respectable, but sadly mistaken.Induce

© 2022 - 2024 — McMap. All rights reserved.