Yes, it's possible.

Creating a database only needs a context to get the application-specific database path.

It's also not the case that Context objects can only be used from the UI thread. UI operations are restricted to the UI thread, but you can do all kinds of things with a Context from a non-UI thread (starting services, sending broadcasts, pushing app widget updates, content provider notifications, etc.)

AFAIK, it's good practice for all worker threads to share the same database object, opened once, rather than use multiple db objects pointing to the same file. The former lets Sqlite handle thread concurrency more efficiently (although I haven't used the latter approach, and can't speak on the specifics why exactly it would be worse).

-- Kostya

18.10.2011 22:10, Rajbir Bhattacharjee ?????:
Hi,
I did a bit of reading on this subject, and there have been quite a few mail chains which have touched upon this as a side note, however, I couldn't find any of them which gave a definitive answer to this particular question. /Is it possible to do sqlite operations in a background thread using the SqliteDatabase and Cursors on android?/

Creating a database requires a Context, which could mean that it is meant to be done in the UI thread only. Can I create a database object in the UI thread using the Context that I have there, and then pass it to the background thread for it to use? I will probably have to do my own synchronization of the database object, but will it lead to any other problems?

My application spawns a lot of background threads (which extend java.lang.Thread). These threads need to read and update an SQL database occasionally. This job cannot be moved to the UI thread (although the UI thread might also update the SQL database independently).

Please let me know if it is OK to create a database object in the UI thread using the UI thread's context, and then use it in the background thread.

If not, then I have another approach in mind (spawning sqlite3 and using a pipe to give input to it and read from it), however I don't want to go that way as it is clumsy.

Any information will be much appreciated.

Thanks and regards,
Rajbir Bhattacharjee
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
Kostya Vasilyev

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to