Search code examples
iosnsnotifications

If add an observer for a notification in the AppDelegate, do I need to bother removing it?


In the AppDelegate's didFinishLaunchingWithOptions:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(contextChanged:)
                                      name:NSManagedObjectContextDidSaveNotification
                                      object:nil];

This is so I can merge changes to the data from other threads.

Question: Do I need to bother removing this listener in applicationWillResignActive or applicationWillTerminate? It doesn't seem like there's a point. I guess I'm asking if it's normal to have listeners like this in the main loop that never get removed.


Solution

  • You can never remove it, but if your app receive a notification (it won't happen in this case) while it is in background the notification will be queued and delivered to the application when it comes up again (if the app isn't killed ofc).

    If don't want notifications that happen when your app is in background to be delivered once it comes up you can remove the listener in the methods you pointed out.

    In this case, actually, it doesn't matter.