ios - 我什么时候应该清除 iOS 应用程序的废弃数据?在启动或终止期间?

标签 ios memory-management application-lifecycle

我使用 CoreData,我正在考虑何时应该删除废弃(或过期)数据。

据我想象,要点如下:

  1. 当应用启动时。
  2. 当应用程序终止时。 (在应用委托(delegate)的 applicationWillTerminate: 中)

我猜,其中一个是无风险的,但我不希望用户在清除过程中等待。 (我知道看门狗异常。如果清除时间超过20秒,我会准备一个 View Controller 等待。)

两个看起来不错。用户看不到等待时间。不过,我不确定这个观点是否合适?有人说后台进程时间有限。

最佳答案

无论哪种方式,您最有可能受益于在辅助线程上运行清理。不过,您很可能无法使用第二个选项:

根据 Apple docs关于applicationWillTerminate:

Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.

您可以使用的一个选项是在 applicationDidEnterBackground: 中清理:但这也有一些复杂性:

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your app is terminated and purged from memory.

这可能是更好的解决方案,您可以通过我链接到的文档深入了解一些建议。不过,在应用程序启动期间执行此操作可能最简单。

关于ios - 我什么时候应该清除 iOS 应用程序的废弃数据?在启动或终止期间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686271/

相关文章:

ios - 关闭搜索栏后,UINavigationBar 后退按钮始终变为蓝色

java - 发送带有附件的邮件的最佳策略,并且 JVM 内存占用较低

iphone - 由于缓存而导致的 UIImage 内存问题

java - 在应用程序类中注册和取消注册 BroadcastReceiver

java - Android 应用程序/Activity 每次启动或恢复时都要完全重新启动?

ios - UISegmentedControl.noSegment 停止与 Xcode 11、iOS 13 配合使用

objective-c - 如何将图像添加到 Core Plot CPT BarPlot 图层?

ios - 在 iOS 中实现音频波形 View 和音频时间轴 View ?

c++ - 类内动态内存分配

ios - 由于我的 View 层次结构,viewWillAppear 显然没有被调用