views:

67

answers:

2

Hi, Experts:

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 could 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!

+1  A: 

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.

ivans
Yes, you're right. Now I will not hesitate about that rule. thank you Ivans.
Elliot Chen
+1  A: 

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.

baswell
Baswell, thanks for your suggestion very much. I'm using FMDB and it's great.
Elliot Chen