Consider the following: I have a Service that is written to the database in AsyncTask . And my activity is reading data from the database (consider a UI thread for simplicity). I am SQLiteOpenHelper database using SQLiteOpenHelper . I create a separate instance in Application onCreate () and then get it in the service and activity. Is there a chance that I will make my database "dead locked"? I used to use ContentProvider for such operations. Although it is based on using a single instance of SQLiteOpenHelper , I decided to simplify my project by excluding ContentProvider .
Consider the code:
public class App extends Application { private OpenHelper openHelper; @Override public void onCreate(){ super.onCreate(); openHelper=new OpenHelper(); } public OpenHelper getHelper(){ return openHelper; } }
In action:
OpenHelper helper=(App)getApplication().getHelper(); SQLiteDatabase db=helper.getReadableDatabase();
And inside Serice, in a separate thread:
OpenHelper helper=(App)getApplication().getHelper(); SQLiteDatabase db=helper.getWritableDatabase();
Would it be safe?
UPD This may be a solution, but not sure how to use it.
source share