PDA

View Full Version : Calling removeObserver before addObserver for a notification.




Marimuthu
Feb 28, 2012, 02:27 AM
Dear All,

In my application's backend (non-UI), I use NSNotificationCenter for communication across various modules.
Ideally I should be register for notification by calling addObserver in the init of the class and removeObserver function in the dealloc method.

But I want to restrict the registration and removal of the notification hence do it at a different point of time.

Occasionally due to my backend application logic, I happen to call again addObserver before calling removeObserver for the same notification. This will results in a crash when I try to post for that notification.

Code used to remove an registered notification.
[[NSNotificationCenter defaultCenter] removeObserver:self];

So I was thinking of ALWAYS CALLING removingObserver before calling addObserver such that I can be absolutely sure that only one instance of the notification is registered.
This will result in removeObserver being called when no notification was being added at all.

It is safe to do so? Are there any drawbacks etc because of this logic?

Thanks in advance.

Regards.



gnasher729
Feb 28, 2012, 02:40 AM
Add a method "wantObserver: with a BOOL argument, which increases or decreases a variable. Register and unregister when the variable changes from 0 to 1. Call wantObserver:YES in init and wantObserver:NO in dealloc.

Marimuthu
Feb 28, 2012, 03:01 AM
Thanks for the reply.
Yes, with this approach, one can ensure that addObserver is called only when the already registered observer was removed.

But for knowledge sake, would like to know if there is something wrong/incorrect if we were to call removeObserver before calling addObserver for a given notification?

gnasher729
Feb 28, 2012, 04:54 AM
Thanks for the reply.
Yes, with this approach, one can ensure that addObserver is called only when the already registered observer was removed.

But for knowledge sake, would like to know if there is something wrong/incorrect if we were to call removeObserver before calling addObserver for a given notification?

What's wrong is that there is no guarantee that it works, and there is certainly no guarantee that it works in 10.8, 10.9, 10.10 and so on. I'd consider it a bug, and Apple might decide at any point to throw an exception if you do it. (Remember, in Objective-C exception equals programming error).

Sydde
Feb 29, 2012, 12:21 AM
But for knowledge sake, would like to know if there is something wrong/incorrect if we were to call removeObserver before calling addObserver for a given notification?

For knowledge sake, if you are encountering this problem, I suggest you review the design of your program to figure out how you can make it go away yourself.

If you are observing a specific object, you should probably bracket the lifespan of that object with the add/remove calls (note that you can register any other object for observation as well as self).

If you are observing a specific named notification, you should bracket the lifespan of the observer with add/remove (usually in init/dealloc for that class).

Notification registration is usually not very dynamic. Consider how you might perhaps generalize the observation method to eliminate the issue (for example, the observer might register for any object and keep a weak ivar or property to identify the current sender of interest).