not calling removeObserver: but not crashing?

I thought this would crash, but it doesn't seem to

I have a UIViewController subclass with the following:


- (void)viewDidLoad {
    [super viewDidLoad];

    NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
    [nc addObserver: self
           selector: @selector(userDataChanged:)
               name: kUserDataUpdated
             object: nil];
}

- (void) dealloc {
    NSLog(@"hello");
}

- (void) userDataChanged: (NSNotification *)notif {
    [self configureWithLoggedInUser];
}


a breakpoint in viewDidLoad suggests addObserver is getting called as expected

a breakpoint in userDataChanged suggests i'm able to trigger my notification.

a breakpoint in dealloc suggests dismissing the view/controller is working as expected.


But I thought the next time the app posted kUserDataUpdated the app would crash.

currently when I:

1. load my viewController

2. dismiss/deallocate it

3. post kUserDataUpdated


nothing bad is happening.


Is it no longer necessary to call removeObserver in dealloc?

or am I avoiding a crash because this is running in the simulator (lots of mem) and I haven't done much else between steps 2 and 3?

thanks,

Mike

Replies

IIRC this kind of notification (selector-based, not block/closure-based) no longer needs to be unregistered before deallocation. I can't find the documentation, but I think it was in iOS 9 or perhaps even iOS 8.

That change was in iOS 9.