简体   繁体   中英

where should i put removeObserver in AppDelegate.m, ios

My AppDelegate class is registered for a particular notification like below

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applyThemeA:)
                                                     name:@"ThemeA"
                                                   object:nil];

    }

And applyThemeA does ( just not much )

- (void)appleThemeA:(NSNotification*)notification {
    NSLog(@"apply themeA");
}

I am placing removeObserver in applicationWillTerminate , but not so sure it is a good way for it.

Question

Is it a good place to place this method in.

I just wanna make sure I am doing the right way. If the question is not appropriate, please dont down vote.Just let me know. Thanks

It's perfectly appropriate. Your listener is created when the app starts, so it should be destroyed when the app exits.

Technically speaking, it's not really needed though. When your application exits, everything will be destroyed, including the listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM