views:

63

answers:

2

Hi, I have some doubts about SQLite... My app uses only one table (encapsulated in a data logic class) to store small data, the connection is opened in didFinishLaunchingWithOptions and finalized in applicationWillTerminate (yes, I know that in iOS4 applicationWillTerminate is only called by iSO if necessary, but I write on database at every change of data).

Now I have to add a second table (and relative class) that will store different data (no join between the two tables) and of normal size (about 1-2 rows for day).

I thought of applying the singleton pattern to share the connection and the statements, but I read that for performance reason it's recommended to use class-local variables to hold the connection.

So what's the best practice? Singleton or two open connection? What solution would you reccommend for my situation?

Thanks

+2  A: 

Singleton for me when stored in the same database.

Small amounts of data shouldn't be a performance bottleneck anytime soon.

Or, of course, go with CoreData. :-)

Eiko
+1  A: 

For such a simple use, a singleton is probably the right answer for the very reasons that @Eiko mentions.

However, it all begs the question: Why aren't you using Core Data?

Using SQLite correctly is actually quite hard and I've seen dozens of very talented/experienced engineers get it entirely wrong without realizing it. Worse, scaling an existing implementation is even harder. Adding concurrency is really really hard with straight SQLite (and the Core Data engineers have expended a huge amount of energy and applied a ton of expertise to support concurrency on top of SQLite correctly).

For such a simple use, Core Data won't be hard to learn and it will leave you with a much more solid and versatile code base.

bbum