以前做了一个应用,里面用到了FMDB,进行每一次操作前,都open,完成操作后都close。因为我是参考他们以前的代码。程序初期没发现什么问题,程序完成后,各种卡顿就出现了!即使我是放在新线程里操作的。仔细检查后发现,程序用在open 和 close上的cpu占有率异常地高,尤其是快速操作进行测试时。后来我查到了下面的帖子,原来,用错了!
http://stackoverflow.com/questions/15720272/when-to-close-sqlite-database-using-fmdb
问题是:
When should you close the connection to an SQLite database (using [db close] in FMDB)? Right now I am closing it after running every batch of related queries, but should I rather close when my app closes? What are the pros/cons of doing either way?
答案是:
I'm the guy who wrote FMDB. Keep it open unless you change your schema. That's the only reason to close it, and constantly re-opening it is a little hit on performance / battery life.
I don't do much iOS programming- so take my answer with a grain of salt. But no, I don't think you need to close it. If the fd is still around in SQLite, then it should all be good. I guess try it, and let us know what you find out :)