15

I'm trying to get one instance of using NSNotificationCenter with addObserver and postNotificationName but I can't work out why it won't work.

I have 2 lines to code to add the observer and send the message in 2 different classes

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newEventLoaded:) name:@"Event" object:nil];

and

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];

If I set the name to nil it works fine becuase it's just a broadcast, when i try and define a notification name the messages never get through.

Affian
  • 3,380
  • 5
  • 22
  • 26

7 Answers7

12

All my code makes use of NSNotifications like so:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView) name:@"ScanCompleted" object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:@"ScanCompleted" object:nil];

The first one is registering the notification and the second posting of the notification.

Venk
  • 5,949
  • 9
  • 41
  • 52
James
  • 5,812
  • 5
  • 26
  • 30
  • 3
    This is exactly what I have yet it refuses to work, leads me to think that the issue is elsewhere but the Notification center seems to be quite self contained code-wise. I haven't a clue where else to look for what could be causing the issue. Threading maybe? Does the iPhone automaticly multi-thread at all? Not that I know of. – Affian Jan 21 '10 at 23:25
  • Does an NSLog statement in the selector (in my case updateView) work properly? If your method doesn't take any parameters try the method name without the : so `[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newEventLoaded) name:@"Event" object:nil];` – James Jan 21 '10 at 23:40
  • All my testing has been done with a NSLog in the in the selected method and it works when I set the notifier's name to nil. I've also tried it both with and without parameters in the method – Affian Jan 21 '10 at 23:49
  • I still don't know why it wasn't working but after a bit of refactoring it's working fine now with the same 2 lines of code as per the documentation. – Affian Jan 25 '10 at 21:19
  • 4
    Gah, what kind of refactoring did you end up doing? I'm having the same issue and it's mind numbing. I even added the postNotification directly after registering, with logging statements & breakpoints to verify those lines are executing.. and nothing – powerj1984 Aug 10 '12 at 20:15
  • Make sure you're not calling removeObserver on the entire view itself, only on the notifications you want to remove like keyboardDidHide for example, this way if you leave the view you are still listening. – Stu P. Jun 04 '14 at 18:51
  • 1
    I know it is a quite old answer but just in case it can help somebody to solve the same issue quicker than I did. In my case I stupidly didn't reference strongly the controller that was observing the notification, so it was deallocated before it could listen to it. – Tamara Bernad Jun 17 '14 at 10:38
  • @TamaraBernad couldn't stop saying Thank You! – Ayan Sengupta Jun 12 '16 at 02:15
11

Basically it's all to do with the order of execution. If you've executed postNotificationName before addObserver, then this is an easy problem to have. Use breakpoints and step through the code :)

Your first breakpoint should stop here:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"ScanCompleted" object:nil];

Then here:

[[NSNotificationCenter defaultCenter]postNotificationName:@"ScanCompleted" object:self];

Also, make sure the selector has a colon on. Because it's method signature will be:

- (void)updateView:(NSNotification *)notification;
PostCodeism
  • 1,070
  • 1
  • 12
  • 20
8

I had the same problem. The reason is that I called removeObserver method at

- (void)viewDidDisappear:(BOOL)animated{

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

   [notificationCenter removeObserver:self];

}

So check whether if you had called removeObserver before postNotification.

Tips: You can search the keyword "removeObserver" to find if you had called this function.

Jia Xiao
  • 667
  • 6
  • 5
  • 1
    Please use the [edit](http://stackoverflow.com/posts/27315932/edit) link below your answer to correct the code formatting. – peter.hrasko.sk Dec 05 '14 at 12:51
  • Same thing was happening to me. Missed some code which was actually removing all observers after I'd added the observer. No surprise it wasn't working. – AndyDunn Mar 30 '15 at 13:57
6

Change this:

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];

to this:

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:nil];

If your first notification is registered properly, newEventLoaded should be called.

beryllium
  • 29,669
  • 15
  • 106
  • 125
David Sowsy
  • 1,680
  • 14
  • 13
  • 1
    No luck, I've tried various combinations of the object values and the event values but the only one that I can get working it seems is having the name set as nil, unless I'm missing something in the documentation. – Affian Jan 21 '10 at 22:51
4

I had a similar issue and my problem was due to the notification being called on another thread. This solved my problem.

dispatch_async(dispatch_get_main_queue(),^{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
});
Brian Kalski
  • 897
  • 9
  • 35
2

Have you tried any other names but @"Event" and nil? Just to be sure, you could define your event names in one file and include that into both notification registration and sending. For example:

Header file:

extern NSString * const NOTE_myEventName;

Source file:

NSString * const NOTE_myEventName = @"MyEventName";

Registration:

[[NSNotificationCenter defaultCenter]
 addObserver:self
    selector:@selector(handleMyEvent:)
        name:NOTE_myEventName
      object:nil];

Notification sending:

[[NSNotificationCenter defaultCenter]
    postNotificationName:NOTE_myEventName object:nil];
JOM
  • 8,139
  • 6
  • 78
  • 111
1

I successfully fixed my "NSNotification not being sent when postNotificationName: called" crash.

I found the real bug is in notification message handler.

The postNotificationName and addObserver are all right as the first post of this thread.

Venk
  • 5,949
  • 9
  • 41
  • 52
heMac
  • 1,529
  • 14
  • 14