There is an observer that I need it in all of app life cycle, shall I ever remove it?
I think GC
will remove it after app is closed, am I right?
If yes, then when shall I remove it? in deinit
?

- 17,993
- 23
- 107
- 210
-
Have a look at [this](http://stackoverflow.com/a/15941336/4539192). This is provided you are giving iOS 8 support, iOS 9 onwards it is no longer necessary, you can read more [here](https://developer.apple.com/library/content/releasenotes/Foundation/RN-Foundation/index.html#10_11NotificationCenter). – Rikh Jan 17 '17 at 05:20
-
Great, please send this as an answer so I accept it. – AVEbrahimi Jan 17 '17 at 05:32
1 Answers
If you are providing support to iOS 8 and before. You will have to remove the observer inside dealloc
or viewWillDisappear
. A more detailed answer can be found here.
If you are providing support from iOS 9 onwards, it is no longer necessary to manually remove the observer. From apple docs:
In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated. If the observer is able to be stored as a zeroing-weak reference the underlying storage will store the observer as a zeroing weak reference, alternatively if the object cannot be stored weakly (i.e. it has a custom retain/release mechanism that would prevent the runtime from being able to store the object weakly) it will store the object as a non-weak zeroing reference. This means that observers are not required to un-register in their deallocation method.
A more detailed explanation can be found here.
Note: However be careful when using block-based notifications as mentioned in the doc linked above.