sqlite3 with iPhone multitasking - Do I need to properly close database?
I keep my database open for the life of the app and close it on termination. When iOS 4.0 multitasking puts my app into the back开发者_运维知识库ground, I save the data but don't close the database. I believe this means that my app will always eventually get purged from memory without ever properly closing the database (or running finalizing statements).
Everything seems to work fine, but I'm just wondering... is it OK to never close the database properly?
I would recommend you close the database cleanly when your app transitions into the background. The reason for this is that your app can be killed by the system in order to preserve resources.
If this happens, your app doesn't get a chance to exit cleanly, and instead is sent the SIGKIL signal, halting your app immediately, regardless what state it's in.
Closing the database, finalising your statements and committing any changes will ensure that your state and data are safe when/if your app is forcibly quit.
精彩评论