我在AppDelegate.h文件中添加了这段代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(saveDataAndSettings)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
我通常写这个:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
如果我将UIViewController类设置为观察者(非ARC)。
我很想知道,如果我在AppDelegate中这样做,我还是应该插入这一行:
[[NSNotificationCenter defaultCenter] removeObserver:self];
在applicationWillTerminate:方法。
因为该应用程序将被终止,这样做是否有意义?
提前致谢
答案 0 :(得分:2)
实际上,不需要在app委托的dealloc
或applicationWillTerminate:
进行太多清理。应用程序即将消失。
应该关闭/保存文件以及其他类似类型的清理工作,但在即将退出的过程中清理内存或观察者方面没什么意义。
答案 1 :(得分:0)
如果您在项目中使用非ARC方法,最好手动取消分配对通知的引用,如下所示:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}