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

大憨熊 提交于 2019-12-22 05:35:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9560419/if-add-an-observer-for-a-notification-in-the-appdelegate-do-i-need-to-bother-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!