Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Marimuthu

macrumors member
Original poster
Oct 18, 2010
43
0
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.
Code:
 [[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

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors member
Original poster
Oct 18, 2010
43
0
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

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.