views:

672

answers:

1

Can anyone clarify/elucidate the situation with respect to -[NSNotificationCenter addObserver:selector:name:object:]?

  • What types of references are kept by the notification center of the 'observer' and 'object' arguments?

  • What are the best practices for removing observers from the notification center?

  • What are the special concerns for multi-threaded applications, especially with respect to the 'object' argument?

  • What are the differences in behavior of this method in GC and non-GC environments?

  • Are the any significant differences (from a client perspective) between mobile and desktop environments in this method's behavior?

Also, any pointers to existing articles which cover this would be greatly appreciated. I Googled, but was surprised to find little in-depth discussion of these issues (although maybe I didn't use the right magic keywords).

+5  A: 

what types of references are kept by the notification center of the 'observer' and 'object' arguments?

I believe a weak reference, though that's just from memory (no pun intended).

what are the best practices for removing observers from the notification center?

Always remove the registered object from the notification center before they're released. The object's dealloc method is a good place for this if it set up the registration itself, or when you release it if another object is managing the notification subscriptions. Keep this in mind and the above won't matter.

what are the special concerns for multi-threaded applications, especially WRT the 'object' argument?

NSNotificationCenter works fine on threads, but if you send a notification from a background thread, the object will receive it on that same thread. Because of this behavior you should use a different approach if you're updating the UI or doing anything else that's not thread safe (or, dispatch the notification from another method on the main thread).

what are the differences in behavior of this method in GC and non-GC environments?

I don't remember hearing of anything that you need to worry about, though I haven't used the GC much yet.

are the any significant differences (from a client perspective) between mobile and desktop environments in this method's behavior?

Not that I've heard of, no. When you register your object you can choose to register for all notifications or only notifications from a certain object. If you're using notifications heavily the latter may be a little faster, but always test to be sure.

Also, any pointers to existing articles which cover this would be greatly appreciated. I googled, but was surprised to find little in-depth discussion of these issues (although maybe i didn't use the right magic keywords).

I think it's more because NSNotificationCenter is pretty easy to use, in general. If you're worried about certain cases, don't be afraid to write a quick test app!

Marc Charbonneau
thx marc. good stuff.
Todd Ditchendorf
Specifically when using NSNotificationCenter under GC you do not need to issue removeObserver: messages and when your object is finalized any references the center has to it will be zero'ed out.
Ashley Clark