On 2 Mar 2011, at 5:32pm, Philip Graham Willoughby wrote:

> I would like to close the database connection if it is safe to do so when my 
> app is suspended, so that the -wal file can be erased saving the user some 
> space, and also because I hear that improves the efficiency of subsequent 
> queries.

The answer to your question depends on what is 'safe' for it, but you already 
figured out the best I can come up with: issue a 'BEGIN' command.  If that 
succeeds, nothing important is going on, so you can do an 'END', then close the 
handle.  If instead it fails, the result code returned may indicate you're 
already in the middle of a transaction, so it's not 'safe' to close the handle. 
 Or it may indicate an unexpected SQLite problem you have to clear up after.

But because of some intricate details about how iOS works, I have to suggest 
this: don't ever hold a database connection open while your app is suspended 
unless your app has required functionality while suspended (e.g. a GPS-tracking 
app).  While it is technically safe to keep a file open while your app is in 
the background, you probably don't want to be using up resources while your 
user doesn't care about your app.  Close the connection when you get the 
'DidEnterBackground' notification and reopen it when you get the 'wake' 
notification.  The standard problem scenario is this: the phone battery may run 
down before your application returns to the foreground.

Simon.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to