0

My application has :

 Activity A that reads from sqlite database
 Service with notification that writes to the database
 on clicking Notification, Activity A opens up

the reading by ActivityA is very small task(in reference to time taken to read) but the writing by the service to the database is very long(it sometimes takes 5-10min)

now when the service is running and i click on the notification, ActivityA that has to read from the database cannot perform its reading as there is already a service writing to that database.

so activityA has to wait (for 5-10min) to read from database.

on researching further i came across this

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#beginTransactionNonExclusive()

when i try to implement this in my method inside sqliteopenhelper class i get error as my application uses min api 10. so how do i get this method working for api 10 or is there anyother way to have parallel database access ?

Maulik Sheth
  • 584
  • 3
  • 10
  • Maybe you can pause writing when your Activity is in the foreground and needs the data set already in DB. – Nitin Sethi Oct 02 '13 at 13:09
  • are your service and activity in 2 different process ? if not, simply share a singleton instance of your database. – njzk2 Oct 02 '13 at 13:10

1 Answers1

0

is there anyother way to have parallel database access ?

I think there is no special way how to achieve it. You should use classic Java synchronization for synchronized access to your database.

Most important thing is that you have to make sure you have only one connection to database (you can't write/read from two different connections in the same time). And try to think about an usage of Singleton. In this case (and also in others) it's very efficient and clean solution and you can avoid many problems with access to db.


You mentioned that your task can last 5-10 min. In similar cases every user should know that you are performing some calculations in the background e.q. show some progressDialog, progressBar or simply start animation of image.

If you are showing some data for example in List this is good reason to use lazy loading.

Have look also at these articles:

Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106