开发者

What's the proper way to use sqlite on the iPhone?

Can you please give some suggestions on sqlite using on the iPhone? Within my application, I use a sqlite DB to store all local data. Two methods can be used to retrieve those data during running time.

1, Load all the data into memory at initialization stage. (More memory used, less DB open/close operation needed)

2, Read corresponding records when necessary, free the occupied memory after using. (Good habit for memory using, but much DB open/close operations needs).

I prefer to use method 2, but not sure whether too many DB opening/closing operations co开发者_Go百科uld affect app's efficiency. Or do you think I can 'upgrade' method 2 by opening DB when app launches and closing DB when app quits?

Thanks for your suggestions very much!


First of all: use FMDB to access SQLite!

Then, create your own singleton "MyDB" class.

Every time you need the database, you do [MyDB instance] to get your FMDB instance.

That way you only have one DB open (in didFinishLaunching) an you close it when your app exits.

That's far and away the best way to use SQLite on the iPhone.

The other option is to use CoreData, something I find great for when you start with an empty database, but FMDB/SQLite works best for me if I have a set of data that's read-only.


Apple seems to suggest that you avoid preloading all the data during the startup in order to ensure faster and smoother startup. Supposedly you should only load data when/if the user needs it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜