0

I've strange problem. In ViewController.m, I post notification after successful save and perform Segue to ListViewController. In ListViewController.m, I set up observer in viewDidLoad and declare method for handling.

The problem is that handler method is called but no code inside is executed! Any idea why?

// ViewController.m
if (success) {
    [[NSNotificationCenter defaultCenter] postNotificationName:kAHPlistSavedSuccessfully object:self];
    [self performSegueWithIdentifier:kAHDetailToListSegue sender:self];
}

// ListViewController.m
- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(plistSavedSuccessfully:) name:kAHPlistSavedSuccessfully object:nil];
}

- (void)plistSavedSuccessfully:(NSNotification *)notification
{
    NSLog(@"notification %@", notification);
    [self someMethod]; // not called !
}
MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
alexhajdu
  • 410
  • 5
  • 13
  • this is log message from plistSavedSuccessfully: notif = NSConcreteNotification 0x1f5406f0 {name = plistSavedSuccessfully; object = } – alexhajdu Feb 13 '13 at 23:43
  • If it is printing that NSLog, it should execute `someMethod` as well. What is inside `someMethod`? Did you try adding an NSLog inside that and check? – iDev Feb 13 '13 at 23:44
  • @ACB yes, NSLog in someMethod works, but If I try to modify ListViewControllers outlets it doesn't work – alexhajdu Feb 13 '13 at 23:54
  • Did you try with breakpoints to see it first calls `addObserver` and then it calls `postNotificationName`? – iDev Feb 13 '13 at 23:56
  • Show the code for someMethod, and point out what specific line is failing. – jsd Feb 13 '13 at 23:56

1 Answers1

1

I can spot a few things that might be at fault.

Firstly, you post the notification before you perform the segue to the list view controller, so it's possible that the notification is posted before the list view controller adds itself as an observer for the notification. (Unlikely because the log statement is printing, but still possible in some cases).

The second thing is that notifications are received on the same queue as the one they are posted on. Is it possible that your post notification code is called on different queue to the main queue? This might cause problems if someMethod does any work with code that isn't thread safe. You should verify which queue your post notification code is called on, and either ensure that it is called on the main queue, or in your handler, dispatch the call to someMethod to the main queue using dispatch_async.

Jasarien
  • 58,279
  • 31
  • 157
  • 188