Removing a NSNotificationCenter observer in iOS 5 ARC
Asked Answered
R

3

21

I have an iOS 5 ARC-based project, and am having difficulty about where I should be removing the observer for the NSNotificationCenter observations which I have registered within a UIViewController. Similar posts on SO have said this should be done in the -dealloc method. Even though this method is not required in ARC projects I have added it with the following code:

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

As a test, I open the UIViewController (within a UINavigationController), do some things which trigger the notifications, and then pop it off the stack by tapping the Back button. I then reopen the UIViewController, and do some more things to trigger the notifications, but notice that each callback is being called twice - an indication that the previous notifications have not been deregistered. Repeating this procedure just causes each callback to be called more than more times, so they appear to never be deregistering.

Any help would be appreciated!

Recrement answered 4/12, 2011 at 2:42 Comment(4)
I had the same problem - infuriating. After hours of debugging, a clean + build 'fixed' the problem. All the while I was stepping through the dealloc and call to -removeObsever, on both the iPhone and simulator. So if anyone else sees this rare problem, rebuild first.Metronymic
How did you register in the first place? You need to show more code.Cherlynchernow
Does the NSNotification center retain the observer?Velocipede
In fact dealloc will never be called when using ARC. This is as designed.Sideburns
M
7

It's pretty clear your dealloc method isn't being called (nor is the removeObserver call).

Why not remove your UIViewController's observer in the viewDidUnload: or viewWillDisappear: methods?

Mediatorial answered 4/12, 2011 at 2:45 Comment(2)
viewDidUnload: is only called in low memory conditions, as far as I know. I also need the notification callbacks to still be fired if the view is off-screen, so that means I can't use viewWillAppear: and viewWillDisappear:Recrement
b.t.w., if you're popping your view controller off the stack, that should be deallocing it. If it isn't, is something else retaining your view controller? Check instruments to be sure. Oh, and here is a related question that might be helpful for youMediatorial
O
7

If your dealloc isn't being called, it's likely because someone is still holding a reference to the view controller. Perhaps you need to mark something as __weak? You can use the allocations instrument to help track down what's holding on to your view controller.

Obscurity answered 4/12, 2011 at 3:1 Comment(0)
E
3

"I also need the notification callbacks to still be fired if the view is off-screen" -> you may need to register UIApplicationWillEnterForegroundNotification. If so, let try this:

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"viewWillAppear");
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    NSLog(@"viewWillDisappear");
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"applicationWillEnterForeground");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
    // do your stuff here
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"applicationDidEnterBackground");
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationWillEnterForeground:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
}

The idea is adding or removing UIApplicationDidEnterBackgroundNotification whenever coming in and out of your screen. We just register UIApplicationWillEnterForegroundNotification when the app enter background and remove once it's back. Be noticed that we just remove UIApplicationDidEnterBackgroundNotification when viewWillDisappear.

My dealloc() is not called by somehow, so I found this way, hope it useful for you too.

Enjoy :)

Exile answered 12/9, 2012 at 3:57 Comment(6)
you forgot to call [super ...] in those. Per documentation.Disembark
Are they required? I don't see any problem without them since I am using this code in my app.Exile
You'll see a problem when the super object will perform its own actions. Get in the habit of doing it, because the doc says so. Otherwise one day you might get into weird issues.Disembark
Thanks Greg, you are right. They mention in the api doc developer.apple.com/library/ios/#documentation/uikit/reference/…Exile
@doraemon Yes, you will sooner or later run into problems if you do not call super in these methods. They are in fact doing some "magic stuff". I have seen several problems that have been solved by adding calls to super.Sideburns
Instead of adding and removing these notifications, why not just add them in init or viewDidLoad and remove all observers in dealloc? Or if you really must have them removed when the view disappears, add them both in viewWillAppear and remove them in viewDidDisappear. No need to juggle :)Myopic

© 2022 - 2024 — McMap. All rights reserved.