gpt4 book ai didi

ios - 我应该在 applicationWillTerminate 中删除观察者吗?

转载 作者:可可西里 更新时间:2023-11-01 05:44:09 25 4
gpt4 key购买 nike

我在 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: 方法中。

因为应用会被终止,这样做有意义吗?

提前致谢

最佳答案

实际上,不需要在应用委托(delegate)的 deallocapplicationWillTerminate: 中做太多清理工作。应用程序进程即将消失。

诸如关闭/保存文件和其他类似类型的清理应该完成,但在即将退出的进程中清理内存或观察者没有什么意义。

关于ios - 我应该在 applicationWillTerminate 中删除观察者吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com