jeudi 16 avril 2015

SQLite: Query returning old/cached data

We've got an activity that a list of items and their statuses. When the user clicks on the item, another activity launches where the user can perform certain tasks that change the status of that item. When the activity finishes and the previous one resumes, the items and their statuses are queried from the database and listed (the query basically does a count(*), so it should find the record that was just inserted).


The problem is some users have been reporting that sometimes the item aren't updated, so they go back and perform the task again resulting in duplicate transactions which should not be possible under normal circumstances (we can confirm this from the data on the server).


For some reason I cannot replicate the behaviour at all (on an actual device and emulator). But we know the query is returning old data because of user feedback and the data we receive. By my estimate it happens around 3% of the time.


Does this have to do with a SQLite setting, or is there a bug in our code?


This is basically what the code does:



// Insert new record
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

processContentForDeviceStock(contentValues, object);

db.insert("table", null, contentValues);
db.close();

new AlertDialog.Builder(
CurrentActivity.this)
.setTitle("Success")
.setMessage("Insert succesful")
.setPositiveButton("OK",
new OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
setResult(RESULT_OK);
// We are done, go back to the previous activity
finish();
}
}).create().show();


When the List activity resumes:



SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery(listQuery, null);
res.moveToFirst();

while(!res.isAfterLast()) {
// Build our item list
/*
...
*/
res.moveToNext();
}

res.close();
db.close();

// List the items and their statuses
/*
...
*/

Aucun commentaire:

Enregistrer un commentaire