当应用程序在后台被杀死时如何执行清理?
我的应用程序创建了一些我在整个生命周期中需要的临时文件。我最终需要处理这些文件,这样它们就不会占用设备上的空间。我想我可以在应用程序退出时删除它们,但由于引入了多任务处理,生命周期并不像以前那么简单。我的主要问题是:当我的应用程序进入后台并稍后被杀死时,它是否收到 applicationWillTerminate:
?或者只是从记忆中删除?
当应用程序进入后台时,我无法进行清理,因为它可能在操作过程中进入后台,并且一旦返回前台就需要临时文件。我认为我可以在启动时清理文件,但无论如何我对 applicationWillTerminate:
行为感兴趣。
My application creates some temporary files that I need during its whole lifecycle. I need to dispose of those files eventually, so they don’t take up space on the device. I figured I could delete them when the application quits, but since the multitasking has been introduced the lifecycle is not as simple as it used to be. My main question is: When my application moves into background and gets killed later, does it receive applicationWillTerminate:
? Or is it just wiped from the memory?
I can’t do the cleanup when the application moves into background, since it could be backgrounded in the middle of an operation and would need the temporary files once it gets back on foreground. I think I could clean the files upon startup, but I am interested in the applicationWillTerminate:
behaviour anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,在支持多任务的 iOS 4 设备上不会调用
applicationWillTerminate:
。当您的应用程序终止时,通常意味着设备内存不足,因此即使did被调用,也不能说您有足够的可用内存来完成任务。Apple 建议您在应用程序进入后台时进行清理。如果您需要几秒钟,您可以请求额外的时间来执行此操作(这是向所有应用程序开放的少数多任务场景之一)。
您的其他选择是在任务完成时(无论何时)或应用程序启动时进行整理。我想可能还有其他排列...
No,
applicationWillTerminate:
is not called on multi-tasking enabled iOS 4 devices. When your app is terminated it usually means that the device is running out of memory, so even if is did get called there's no saying that you'd have enough memory available to complete the task anyway.Apple recommend that you clean up when your app goes into the background. You can request extra time to do this if you need a few seconds (this is one of the few multi-tasking scenarios open to all applications).
Your other options would be to tidy up when the task is finished (whenever that is) or when the application is started. I guess there may be other permutations...
互联网上说,当应用程序在后台被杀死时,不会调用 applicationWillTerminate: 。另一种方法是保存一些信息(在 NSUserDefaults 等中),即您打开的临时文件,并在启动后检查这些文件是否保留,然后删除它们。
另外,您还可以了解 iOS 如何杀死您的应用程序(我认为这是通过发送信号来完成的,但我不确定)并向您的应用程序添加信号处理程序。
Internet says, that applicationWillTerminate: is not called while application is killed in background. The other way is to save some info (in NSUserDefaults, or so), which temporary files you opened, and after startup check if those files are left, and delete them.
Also, you can find out, how iOS kills your application (I assume, that this is done by sending signals, however i'm not sure) and add signal handler to your app.