我使用 CoreData,我正在考虑何时应该删除废弃(或过期)数据。
据我想象,要点如下:
- 当应用启动时。
- 当应用程序终止时。 (在应用委托(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/