samedi 28 février 2015

Android cursor sometimes returns empty even when the data exists

I have an android application that first fetch some data from the web (any updates that might available for an Item) and write the data and after that read the fetched data from the database. The problem is in some cases the reading operation with android cursor returns an empty cursor but we know that the data exist because you updated that exact row some seconds ago!


In fact you can't request an online update for some data that not exist. I searched a lot but no result. I thought I might be from reading and writing at the same time but It wasn't.


below is the code that do this work :



protected Biz doInBackground(Void... params) {
Bundle bundle = BizDetailsActivity.this.getIntent().getExtras();
// bizID = bundle.getLong(BIZ);

BizDataSource ds = new BizDataSource(BizDetailsActivity.this);
ds.openWritable();
long ID = bundle.getLong(BIZ);
if (NetworkHelper.IsConnected(BizDetailsActivity.this)) {
// HERE we write data if avaiable
getBizOnline(ID, ds);
}
//read data from database
biz = ds.getBiz(ID, data.getCurrentUser());
ds.close();
return biz;
}


Code for writing/updating data :



public long updateBiz(long ID, String title, String description,
String address, String phone, String mobile, String fax,
String owner, long city, String website, double latitude,
double longitude, int addDate, String expireDate, long gallery,
String logo, float rate) {

ContentValues values = new ContentValues();

values.put(columns[1], title);
values.put(columns[3], description);
values.put(columns[4], address);
values.put(columns[5], city);
if (gallery > 0) {
values.put(columns[6], gallery);
}
values.put(columns[7], latitude);
values.put(columns[8], longitude);
values.put(columns[9], phone);
values.put(columns[10], mobile);
values.put(columns[11], fax);
values.put(columns[12], website);
values.put(columns[13], owner);
values.put(columns[14], logo);
values.put(columns[15], addDate);
values.put(columns[16], expireDate);
values.put(columns[19], rate);

long result;
try {
result = database.updateWithOnConflict("Bizes", values, "ID = ?",
new String[] { ID + "" },SQLiteDatabase.CONFLICT_FAIL);
} catch (Exception e) {
Log.d("UPDATE_BIZ", e.getMessage());
result = -1;
}
return result;
}


Code for getting data :



public Biz getBiz(long id, long UserId) {
Gallery gallery = null;
City city = null;
BizCategory category = null;
Cursor c = database.query("Bizes", columns, "ID=?", new String[] { id + "" }, null, null, null);
if (c.moveToFirst() == false) {
// here we got empty cursor !!!!
c.close();
return null;
} else {
....
}
}

Aucun commentaire:

Enregistrer un commentaire