views:

182

answers:

1

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.

+1  A: 

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.

bbum
Thank you bbum. How can I ensure that my sending view controller and observing view controller (they are parent/child via navigation controller) are on the same thread? Or is it even possible they are on different threads?
Greypoint
The notification will always be handled on the same thread. That is the problem. If the view controllers aren't thread safe, they should always be messaged on the main thread and, thus, all notifications *must* be posted on the main thread.
bbum
Hm, two things make me think these are actually on the same thread... I modified my post notification to use: "performSelectorOnMainThread" (forcing it to the main thread) but it still doesn't work. Second, I used "Instruments" to track thread activity and it appears both are on the main thread (if I understand what I see). Frustrating. Any way to trace NSNotifications or somehow validate that my addObserver has actually been added?
Greypoint
Edit: It works if I have a test posting in the same viewcontroller that is the observer so now I really can't tell for certain whether it's a threading issue or not.
Greypoint