Where is the best place to invoke `removeObserver:name:object:`

411 views Asked by At

Where is the best place to invoke removeObserver:name:object: since the dealloc method does not always be executed as mentioned in NSObject class reference ??

1

There are 1 answers

1
BoltClock On BEST ANSWER

If you're referring to this note:

Important: Note that when an application terminates, objects may not be sent a dealloc message since the process’s memory is automatically cleared on exit—it is more efficient simply to allow the operating system to clean up resources than to invoke all the memory management methods.

It says dealloc is typically not guaranteed to be called only on application termination. So even if dealloc isn't called, the resources used by your application will still be cleared by the OS. That means all your objects will be gone anyway because your application isn't there anymore.

Therefore, the best place to remove a notification observer from the notification center is still within the observer's dealloc method.