CBGrey
CBGrey

Reputation: 643

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.

Upvotes: 8

Views: 1924

Answers (2)

Vilém Kurz
Vilém Kurz

Reputation: 3410

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

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

Upvotes: 1

bbum
bbum

Reputation: 162712

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.

Upvotes: 1

Related Questions