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
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.
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.
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.)
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.
Just for Memory
sake, you should properly remove it in the -dealloc
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With