banditKing
banditKing

Reputation: 9579

Removing observer of NSNotificationCenter in Singleton Objective C

Quick question:

I have a singleton class which is a registered for several NSNotifications. Since Singletons last over the app's lifetime.

Do I have to implement

  [NSNotificationCenter defaultCenter] removeObserver:self] 

In my singleton class?

Whats the right way to deal with NSNotification center in Singletons in iOS?

Thanks

Upvotes: 8

Views: 2419

Answers (3)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

If you want that your singleton listen to notification throughout your app then there is no need of removing it.

If you want that at certain point your singleton should stop receiving notifications then you can add a method in your singleton and can call it where needed

- (void)removeObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self] ;
}

Upvotes: 0

Kyle Fang
Kyle Fang

Reputation: 1209

Just for Memory sake, you should properly remove it in the -dealloc.

Upvotes: 2

lnafziger
lnafziger

Reputation: 25740

No, you don't need to stop observing in this case. The only time that the memory used by a true Singleton will ever be deallocated is when the program exits. When the program exits, it goes ahead and deallocates all of the memory and resources that are being used anyway.

Upvotes: 10

Related Questions