0

I am using FMDB lib to store messages (chat application). It's a objective-c wrapper for sqlite3. I have singleton where all queries implemented.

Just example:

NSString *query = [NSString stringWithFormat:@"UPDATE table_name SET some_col = some_value;"];

[database open];
[database executeUpdate:query];
[database close];

So, should I open database and close for every query, or open only once when my singleton initialized and close when application terminated?

Also, What's the best way to store data (messages)?

Zhans
  • 273
  • 2
  • 11

1 Answers1

1

You should open and close for every query. It looks like a lot of work for the device but it will save you from tons of bug.

Pham Hoan
  • 2,107
  • 2
  • 20
  • 34
  • Could you write in detail, What kind of bug? For my knowledge – Zhans Apr 19 '14 at 07:26
  • that's I've learned when developing in C# and Java. I think it applies to every progamming language. FYI, opening a database is expensive in terms of both time and resources. So the fewer times you have to open it the better performace your program will achieve. The connection will be return to the pool and reuse later. The bug might be because I was noob at that time (fixed that by close the connection, you should try it yourself). – Pham Hoan Apr 19 '14 at 07:37
  • What about way to store data? – Zhans Apr 19 '14 at 09:24
  • It depends on what you want to store. For app setting, you should use `NSUserDefaults`. For private data like password, you can use `keychain`. For general data, using `sqlite3` with `FMDB` is good. If you want to be more advanced, you could try `Core Data`. – Pham Hoan Apr 19 '14 at 10:32
  • Oh, sorry :D Under data I meant a message. I am writing chat based application (first time) – Zhans Apr 19 '14 at 10:49
  • so `sqlite3` plus `FMDB` is the best IMHO. Easy to use and fast. I'm not sure about `CoreData` (haven't used it yet) but I think using it in this case is an overkill. – Pham Hoan Apr 19 '14 at 11:08