I am trying to make multithread use of one sqlite database on Android. I have the following situation :
Activity A : it has 10 threads which use DB ,but uses only one SQLiteDatabase
Activity B : has 10 threads which use DB ,but uses only one SQLiteDatabase
(the two SQLiteDatabase objects of the Activities are different)
The logic execution is the following :
- A calls B
- B inserts into the database and returns to A
- A display the result
- A try to erase what B inserted but fails .
A can erase old things ,but the new objects that B just inserted fail,if I restart the application ,A is able to erase the objects that previously couldn't. Should I use the same SQLiteDatabase for the two Activities ,and if is this the case how to pass this object to Activity B ?
Activity A thread code :
private class Delete_Task extends AsyncTask {
private Context mContext;
private DatabaseManager mDB;
public DeleteEvent_Task(Context mContext,DatabaseManager DB) {
this.mContext = mContext;
this.mDB = DB;
}
@Override
protected Void doInBackground(Long... params) {
long id = params[0];
this.mDB.delete(id);
return null;
}
}
Activity B thread code :
private class Update_Task extends AsyncTask< Object, Void, Void > {
private Context mContext;
public Update_Task(Context mContext)
{
this.mContext = mContext;
}
@Override
protected Void doInBackground(Event... params) {
Object e = params[0];
DatabaseManager DB = new DatabaseManager(this.mContext);
if (e.getId() <= 0)
{
DB.add(e);
}
else
{
DB.update(e.getId(), e);
}
DB.close();
return null;
}
}