In one of my Android applications,
- I have the SyncAdapter
running in a different process, which actually queries the data from sqlite db and pushes the data to server.
- SqliteDatabase
is singleton and there is only instance of it across the application.
But, right after I install the application, two SqliteDatabase
instances are getting created. One for my application and one for the background process(sync). This way two instances are created and they are acting on the same db.
In these scenarios, if the two instances at a time try to insert the db, one of the request will throw error.
Is this the correct way to handle (CRUD db) when having multiple processes ?
Code where the single instance of db is created
public class SqliteStorage extends SqliteOpenHelper {
public SqliteStorage(Context context) {
super(context, DB_NAME, null, DB_VERSION);
db = getWritableDatabase();
db.setVersion(DB_VERSION);
}
public static synchronized SqliteStorage getInstance() {
if(mDBStorage == null) {
mDBStorage = new SqliteStorage(MyApplication.getContext());
}
return mDBStorage;
}
}