Removing a NSNotificationCenter observer in iOS 5 ARC

16,147

Solution 1

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?

Solution 2

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.

Solution 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 :)

Share:
16,147
Skoota
Author by

Skoota

Updated on June 10, 2022

Comments

  • Skoota
    Skoota about 2 years

    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!

  • Skoota
    Skoota over 12 years
    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:
  • Michael Dautermann
    Michael Dautermann over 12 years
    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 you
  • GregJaskiewicz
    GregJaskiewicz over 11 years
    you forgot to call [super ...] in those. Per documentation.
  • thanhbinh84
    thanhbinh84 over 11 years
    Are they required? I don't see any problem without them since I am using this code in my app.
  • GregJaskiewicz
    GregJaskiewicz over 11 years
    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.
  • thanhbinh84
    thanhbinh84 over 11 years
    Thanks Greg, you are right. They mention in the api doc developer.apple.com/library/ios/#documentation/uikit/referen‌​ce/…
  • Johan Karlsson
    Johan Karlsson over 9 years
    @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.
  • Chris Nolet
    Chris Nolet over 9 years
    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 :)