3

So I'm new to NSNotifications, I am wondering what the scope is. I.e. If I have an Application Delegate Class, and it is the receiver of a notification:

-(id)init
{
    [ super init];
    if (!self) return nil;

    // Add to our notification
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveUpdateRequest:) 
                                                 name:@"RequestStatusUpdate"
                                               object:nil];
    return self;
}

And has this method run on receive:

- (void) receiveUpdateRequest:(NSNotification *) notification
{
    // Check the Notification Name
    if ([[notification name] isEqualToString:@"RequestStatusUpdate"]){
        NSLog (@"Recieved Update Status!");
    }
    else {
        NSLog(@"Recieved Notification: %@",[notification name]);
    }

}

Can I post a notification like so:

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

From another object instance any where in my App?

Even for instance an object that instantiated by virtue of a NIB being loaded:

summaryWindow   = [[SummaryWindowController alloc] initWithWindowNibName:@"SummaryWindow" owner:globalStatusController];

Do I have to have anything else configured in my summaryWindow Class to be able to call the postNotificationName method.

Or put a different way is the [NSNotificationCenter defaultCenter] global for all instances of all objects in my Application, I would assume thats how its suppose to work but currently when I call this method via an IBAction in my SummaryWindow , the notification does not seemed to be received.

I have tested both [NSThread currentThread] and the default Notification center and it does look like I'm in the thread and the same notification center ( which I think is always global). I am only looking into the thread thing as its come up on a few other threads.

2011-08-22 20:57:11.452 AppName[23102:1307] Using Default Notification Center: <CFNotificationCenter 0x10012c900 [0x7fff7d302ea0]>
2011-08-22 20:57:20.366 AppName[23102:1307] Using Default Notification Center: <CFNotificationCenter 0x10012c900 [0x7fff7d302ea0]>
Alfabravo
  • 7,493
  • 6
  • 46
  • 82
acidprime
  • 279
  • 3
  • 18
  • 3
    side note: favor external constants over string literals and/or `#defines` – justin Aug 22 '11 at 20:55
  • Thanks, I have just saw some examples on that in this context , great suggestions. – acidprime Aug 22 '11 at 21:54
  • Here is a link to a good constants.m concept for those who find this thread http://stackoverflow.com/questions/538996/constants-in-objective-c – acidprime Aug 22 '11 at 22:14

2 Answers2

2

Wow that was lame, I just found [[NSNotificationCenter defaultCenter] removeObserver:self]; in some earlier code. I had it in dealloc but some how managed to miss it in another NSTask method I was working on.

acidprime
  • 279
  • 3
  • 18
0

Or put a different way is the [NSNotificationCenter defaultCenter] global for all instances of all objects in my Application

Yes.

I would assume thats how its suppose to work but currently when I call this method via an IBAction in my SummaryWindow , the notification does not seemed to be received.

That's because you're registering in init. I'm betting this is Mac, and on Mac the application delegate is almost always instantiated from a nib file. You need to do this work in awakeFromNib.

Note that you generally do not need to check the notification's name. It's generally best to have a different method for each notification callback. You should also always create a string constant for your notification names. It's way too easy to mis-type them.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I tried moving to the awakeFromNib method and I am still not receiving the notifications NSLOGs – acidprime Aug 22 '11 at 21:27
  • Put a breakpoint on your `addObserver:...` and `postNotification:` and make sure they're actually called. – Rob Napier Aug 22 '11 at 22:19
  • Hmm, well I am not really sure how break points would be used to determine is these are actually called. I mean if I add a break point it just stop execution there. I am sure its just a technique I'm not familiar with. I put an NSLog before and after and see them run ( not the receiving method, but the two methods mentioned. – acidprime Aug 22 '11 at 22:43
  • Breakpoints achieve the same goal as NSLog(). If they cause you to stop, then the line of code was reached. So you have an NSLog() statement in -init and it runs? And one immediately before the postNotification? Also make sure that the `addObserver:` runs before the `postNotification:`. – Rob Napier Aug 22 '11 at 23:44
  • Make sure you haven't made a typo in the notification names. This is one reason why constants are a good idea. – Rob Keniger Aug 23 '11 at 00:31
  • Thanks for Breakpoints tip, that makes total sense. I shifted my code over to a constants class so both classes are now using the same source which should negate typos. In reading similar questions such as [link] http://stackoverflow.com/questions/4577255/nsnotification-postnotificationname-in-appdelegate-but-nsnotificationcenter-in-vi I may need to examine the order, but right at the moment, I don't see how per the NSLogs I am seeing: – acidprime Aug 23 '11 at 01:34
  • 2011-08-22 18:32:05.241 App Name[21250:1307] Adding an Observer for RequestStatusUpdateNotification 2011-08-22 18:32:39.141 App Name[21250:1307] Requesting Global Status Update: (RequestStatusUpdateNotification)... – acidprime Aug 23 '11 at 01:34
  • NSLog(@"Adding an Observer for %@",RequestStatusUpdateNotification); // Add to our notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveUpdateRequest:) name:RequestStatusUpdateNotification object:nil]; – acidprime Aug 23 '11 at 01:37
  • NSLog(@"Requesting Global Status Update: (%@)...",RequestStatusUpdateNotification); [[NSNotificationCenter defaultCenter] postNotificationName:RequestStatusUpdateNotification object:self]; – acidprime Aug 23 '11 at 01:38
  • 2011-08-22 20:39:03.278 AppName[22885:1307] Adding an Observer for RequestStatusUpdateNotification on thread:{name = (null), num = 1} – acidprime Aug 23 '11 at 03:41
  • 2011-08-22 20:39:19.720 AppName[22885:1307] Requesting Global Status Update: (RequestStatusUpdateNotification) on thread:{name = (null), num = 1}... – acidprime Aug 23 '11 at 03:41
  • Looks good to me anyone else? I am just NSlogging [NSThread currentThread] here. – acidprime Aug 23 '11 at 03:41
  • For the record, I did not have to move this from init using the default App delegate. – acidprime Aug 23 '11 at 05:55