NSNotificationCenter is returning nil - Any idea why NSNotificationCenter loses value before callback? [closed]

StackOverflow https://stackoverflow.com/questions/19941285

Question

I have essentially the same code for two different notifications. The first one is returning my data correctly but the second one is nil. This may be a silly question but could it be because I'm using the same NSNotificationCenter for both?

NSNotificationCenter *note = [NSNotificationCenter defaultCenter];
[note addObserver:self selector:@selector(onProcessedReady:) name:@"Processed" object:nil];
[note addObserver:self selector:@selector(onGeneratedReady:) name:@"Generated" object:nil];

This is where I am setting the data for NSNotification callback:

NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:1];

[data setObject:self.templateData forKey:@"Template"];

NSNotificationCenter *templateNote = [NSNotificationCenter defaultCenter];
[templateNote postNotificationName:@"TemplateGenerated" object:nil userInfo:data];

<-- data looks good here.

This is the callback:

-(void) onGeneratedReady:(NSNotification *)note  ///  <-- note is nil
{
    if ([note.name isEqualToString:@"TemplateGenerated"])
    {
        NSDictionary *userData = note.userInfo;
        TemplateData *templateData = [userData objectForKey:@"Template"];
        NSLog(@"what is in userData?");

    }
}

It should be noted that the object 'self.templateData' is an object class I created.

@property (nonatomic, strong) TemplateData *templateData;

Just to be sure that my object 'self.templateData' was not the problem, I tried the following and still the note returning in the callback is nil.

NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:1];    
// [data setObject:self.template forKey:@"Template"];
[data setObject:@"I am so happy" forKey:@"NotHappy"];

NSNotificationCenter *templateNote = [NSNotificationCenter defaultCenter];
[templateNote postNotificationName:@"TemplateGenerated" object:nil userInfo:data];

data looks good here. I have a key/value pair

[0] = @"NotHappy" : @"I am so happy"
  key = (__NSCFConstantString *) @"NotHappy"
  value = (__NSCFConstantString *) @"I am so happy"

This is the callback:

-(void) onGeneratedReady:(NSNotification *)note  ///  <-- note is nil
{
    if ([note.name isEqualToString:@"TemplateGenerated"])
    {
        NSDictionary *userData = note.userInfo;
        TemplateData *templateData = [userData objectForKey:@"Template"];
        NSLog(@"what is in userData?");

    }
}
Was it helpful?

Solution 2

The Real Answer !!!

I was returning an object in NSNotificationCenter but the object did not have a strong pointer and I was losing my object before getting back to the callback.

I hope this post helps somebody in the future :-)

OTHER TIPS

note is just a reference to the [NSNotificationCenter defaultCenter] upon which you're calling the addObserver:selector:name:object: method. There is no problem in using the same reference, actually you could write directly

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

It's not clear what you put in the templateData property, nor where you actually post the notification named "Generated" which notifies self and runs -onGeneratedReady:.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top