3

I'm uisng addObserver:selector:name:object: in viewDidLoad.

And I'm using removeObserver:name:object: in viewWillDisappear:animated: to remove observer.

What will happen if I failed to remove observer by passing wrong parameter to removeObserver:name:object:?
(For example, observer isn't removed if I pass wrong notification to parameter name or wrong object to object or Observer)

If the observer still not nil after calling removeObserver:name:object:, I can find out that removing observer failed because notificationSelector will being called.

But if the observer become nil after calling removeObserver:name:object:, I can not find out whether removing observer failed or not.

Will observers automatically removed when observer become nil?
Or does notification dispatch table of NSNotificationCenter became larger and larger and eventually the app become slow?

EDIT

When I use subclass of UIViewController object for observer, the app doesn't crash after ViewController's dealloc are called.
But when I use a object of other class, the app crashs after the object's dealloc are called.

2 Answers 2

10

Update: From -[NotificationCenter removeObserver:]:

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc method. Otherwise, you should call this method or removeObserver:name:object: before observer or any object specified in addObserverForName:object:queue:usingBlock: or addObserver:selector:name:object: is deallocated.

Old answer:

Observers are not removed automatically. From the NSNotificationCenter Class Reference:

Important: The notification center does not retain its observers, therefore, you must ensure that you unregister observers (using removeObserver: or removeObserver:name:object:) before they are deallocated. (If you don't, you will generate a runtime error if the center sends a message to a freed object.)

You should therefore call

[[NSNotificationCenter defaultCenter] removeObserver:self];

in your dealloc method if you are not 100% sure that the observer was not removed previously.

2
  • thanks. I found that if I use subclass of UIViewController as observer, runtime error doesn't happen. If I use object of other class as observer, runtime error happens. Perhaps, UIViewController calls removeObserver:self in its dealloc. But I don't know how to check if super class really removed observer.
    – js_
    Jul 19, 2012 at 10:50
  • I googled your quote from the (?) reference, but couldn't find it, so that's probably old information. Now it seems this is still correct at this point of time; NSNotificationCenter does not retain the observer, as I get dealloc called on an object without even removing it as observer. Now, of course the right thing would be to remove it in dealloc as per this answer. Still the documentation does not state if the observer is retained or not and is thus a bit unclear IMO.
    – Jonny
    Apr 9, 2014 at 2:53
0

You just need to put in the correct Observer for the Observer to be removed. If you pass the wrong parameter to name or object (or nil), the receiver will not use them as criteria for removal.

All Cocoa programs have a default NSNotificationCenter, so once you remove the observers you shouldn't have to worry about the it taking up more memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.