如果在AppDelegate中为通知添加观察者,我是否需要打扰删除它? [英] If add an observer for a notification in the AppDelegate, do I need to bother removing it?

查看:518
本文介绍了如果在AppDelegate中为通知添加观察者,我是否需要打扰删除它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AppDelegate的 didFinishLaunchingWithOptions

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.

问题:我是否需要在 applicationWillResignActive applicationWillTerminate 中删除​​此侦听器?它似乎没有意义。我想我在询问在主循环中是否有这样的监听器是不正常的,永远不会被删除。

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.

推荐答案

你可以永远不要删除它,但如果你的应用程序在后台收到通知(在这种情况下不会发生),通知将排队并在应用程序再次启动时传递给应用程序(如果应用程序未被杀死) )。

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.

这篇关于如果在AppDelegate中为通知添加观察者,我是否需要打扰删除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆