Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 710
banditKing Avatar asked Feb 02 '13 04:02

banditKing


People also ask

How do I delete an observer in Objective C?

When removing an observer, remove it with the most specific detail possible. For example, if you used a name and object to register the observer, use removeObserver:name:object: with the name and object.

How do I delete a specific observer in Swift?

Removing registered observer For Selector approach, use NotificationCenter. default. removeObserver(self, name: notificationName , object: nil) , to remove the observer. For Block based approach, save the token you obtained by registering for notification in a property.

What is singleton class in Objective C?

A singleton is a special kind of class where only one instance of the class exists for the current process. (In the case of an iPhone app, the one instance is shared across the entire app.)


2 Answers

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.

like image 168
lnafziger Avatar answered Sep 30 '22 16:09

lnafziger


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

like image 37
Kyle Fang Avatar answered Sep 30 '22 15:09

Kyle Fang