I am writing a credit card applicatoin by having an "online mode", that's, information is downloaded, parsed and placed right into a SQLite database when needed. All of this is carried out with a service. The application includes several activities that request the service for any data update (different data with respect to the activity).

Once the user navigates with the activities (without awaiting the plan to finish), it is easy to obtain SQLiteExceptions (message: database is locked).

I figured about using synchronized blocks, but that will pressure the consumer to hold back while loading a brand new activity (that requires database use of load) as the service finishes upgrading. To ensure that appears a stalemate.

An alternative choice may be to steer clear of the update much more the onStop approach to each activity. Obviously, the update is going to be interrupted, but that is not really a large problem. Trouble with this really is that I am unsure how to overcome it.

My real question is, just how canOrought to I handle this?

In case your activity will simply read and never email database like my situation, this is exactly what used to do like a workaround:

  • produce a service ( i.e DatabaseService and employ it like a central indicate access database (i.e open a database connection) to make sure you have only one dbhelper at any given time.
  • all activity and repair which require to gain access to database need to begin a link with DatabaseService
  • make sure that only your DownloadService's thread has the capacity to email database also it should use transaction
  • next, you should use getReadable database to see / make use of the link with steer clear of the download service , etc.

Just make certain that you simply just use 1 dbhelper.