5

I want to check if my view is listening for UIApplicationWillResignActiveNotification or not. If it is listening then I want to remove it during dealloc. Now I was wondering if there is way to do this using objective c ?

I am not trying avoid multiple additions for notifications. Here is bit more explanation of what I am trying to do.

I have custom gridView. I can initialize it with either scaling enabled or scaling disabled. If init with scaling enabled I add itself as observer of UIApplicationWillResignActiveNotification but if its init with scaling disabled then it does not add itself as an observer for that notification. Now, in dealloc I want to remove that gridView as an observer of that notification. So I was wondering if there is way to find out if gridView is listening to that notification or not.

iain
  • 5,660
  • 1
  • 30
  • 51
slonkar
  • 4,055
  • 8
  • 39
  • 63
  • Could you please specify what task are you trying to accomplish? – ivanmoskalev Oct 17 '13 at 20:17
  • 2
    possible duplicate of [How to avoid adding multiple NSNotification observer?](http://stackoverflow.com/questions/5658426/how-to-avoid-adding-multiple-nsnotification-observer) – iwasrobbed Oct 17 '13 at 20:22
  • There isn't a way to check if an observer is already registered. – iwasrobbed Oct 17 '13 at 20:22
  • Would it not be easier to always register your view as an observer, and then in the notification action have the logic for whether or not you are scaling? That way you'll know that you always have to unregister in dealloc – iain Jun 17 '15 at 14:58

3 Answers3

3

I don't know of any way to check what notifications your observer is listening for, but regardless of whether it's listening for UIApplicationWillResignActiveNotification or not, calling:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification];

will cause self to stop listening for that notification, or do nothing if self is not listening for it.

Specifying the name of the notification you want to stop listening for is the best practice, but since you said you're putting this in dealloc, it would also be safe to just do this:

[[NSNotificationCenter defaultCenter] removeObserver:someObserver];
BevTheDev
  • 563
  • 2
  • 9
1

If you want to check in dealloc method, if your view is registered as observer to correctly remove it - you should not. All you need to do is:

[[NSNotificationCenter defaultCenter] removeObserver:myView]

and it will remove observers for all notifications you subscribed

0yeoj
  • 4,500
  • 3
  • 23
  • 41
storoj
  • 1,851
  • 2
  • 18
  • 25
  • 1
    Merely doing [[NSNotificationCenter defaultCenter] removeObserver:myView] is not good programming practice. – slonkar Oct 17 '13 at 20:34
0

NSNotificationCenter does not support this out-of-the-box. You have the same problem with KVO.

Generally one just keeps track of whether or not an object has been registered using a boolean property and unregisters only if this boolean has been set.

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
  • 1
    `NSNotificationCenter` is bit more forgiving in that you can always remove an observer, and it simply doesn't do anything if that object is not an observer at the moment. So you can always safely call `removeObserver:` with `NSNotificationCenter`. However, if you try to remove a KVO observer but the target is not observing, you get an exception. This means, you do need to keep track of your observation state with KVO, but not necessarily with `NSNotificationCenter`. – DarkDust Jun 19 '15 at 08:56
  • @DarkDust Just because you don't *have* to doesn't mean you *should* not do so. – Christian Schnorr Jun 19 '15 at 08:58