How to perform cleanup when application is killed in background?
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 th开发者_C百科e 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.
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...
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.
精彩评论