Elliot Chen
Elliot Chen

Reputation: 378

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

Upvotes: 0

Views: 342

Answers (2)

baswell
baswell

Reputation: 836

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.

Upvotes: 2

ivans
ivans

Reputation: 1485

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.

Upvotes: 1

Related Questions