Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods to debug NSNotificationCenter issues?

I'm having some issues where my posted notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MobileProviderChanged" 
                                                    object:self.selectedProviderID];

Is not being trapped by my observer:

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

The exact same observer works correctly in a different ViewController.

Any tips on methods to debug this further to see what messages are actually posted to the defaultCenter?

Thanks.

like image 838
CBGrey Avatar asked Jan 23 '10 16:01

CBGrey


2 Answers

The more I research this issue I wonder if my problem is that the sending viewcontroller is on a different thread than the observing viewcontroller.

Incorrect multi-threading is almost assuredly the source of your problem. However, the notifications should still be sent and received.

Specifically, a notification will be received on whatever thread it was sent upon. Since you mention that you are mucking about with view controllers in response to the notification, it is quite likely you are doing something on a non-main thread that the UIKit is unhappy about.

like image 198
bbum Avatar answered Nov 13 '22 07:11

bbum


Got the same problem. Solution is quite simple, but not obvious:

Make sure, that the observer still exists during NSNotification delivery.

like image 36
Vilém Kurz Avatar answered Nov 13 '22 06:11

Vilém Kurz