I'm currently experimenting with writing an iPhone app and am having some trouble with understanding how to exit an app properly.
Basically, I have a view controller that is displaying a table of data to the user. At the same time, when the application launches, it spins a new thread to retrieve data from the server and update the table in the background. The data is being written to a local sqlite db.
My concern is that without proper cleanup and termination, it is entirely possible that the worker thread could be terminated at a nasty spot (like, during a write) and leave the sqlite db corrupted. I would like to signal the thread to exit cleanly if the app is asked to terminate. But, as far as I can tell, dealloc isn't even called for most of my objects, so nothing actually exits cleanly. Right now, I'm trying to handle applicationWillTerminate and possibly calling some sort of custom finalize method on one or two important classes.
So...
- Is there a guaranteed window of time that I have to respond and exit cleanly with applicationWillTerminate is called?
- Are there other suggestions for exiting cleanly?