5

I am confused about database open and close operation for FMDB wrapper class.

Is it creating issue if i open database in applicationDidFinishLoading method of AppDelegate class and do not close until application will terminate ?

Thanks.

Nikh1414
  • 1,238
  • 2
  • 19
  • 35

2 Answers2

7

There's no reason to close it unless you change the schema. So keep it open.

ccgus
  • 2,906
  • 23
  • 18
5

From the Official FMDB documentation:

Opening

Before you can interact with the database, it must be opened. Opening fails if there are insufficient resources or permissions to open and/or create the database.

if (![db open]) {
[db release];
return;
}

Closing

When you have finished executing queries and updates on the database, you should close the FMDatabase connection so that SQLite will relinquish any resources it has acquired during the course of its operation.

[db close];

So, every time your query the Database, you should have a pair of open and close calls on your database.

In short, open a DB connection when you require things from database and close it when you are done using the database.

Link to documentation : https://github.com/ccgus/fmdb

Ravi Raman
  • 1,070
  • 9
  • 16
  • so what are the drawbacks if i am not closing database with in whole life cycle of iOS application? – Nikh1414 Jan 23 '13 at 12:46
  • `SQLite will relinquish any resources it has acquired during the course of its operation.`, from the documentation, if you don't close the connection the resources acquired by SQLite will not be freed and your application will have memory issues. – Ravi Raman Jan 24 '13 at 04:45
  • If you are doing lots of queries however, you're better off keeping the database open. There's a hit in performance when you open up a database, and honestly, the resources sqlite takes up isn't very much. Just keep it open. – ccgus Jul 20 '14 at 20:33