Forcing query execution in SQLiteDatabase.query

The issue is that our code often "executes" a query
on a background thread but iterates over the cursor
on the UI thread.  Since we actually do the fetch
on moveToFirst or moveToNext, the query is in reality
often run on the UI thread and causes an ANR.

Change-Id: Ia561135e974a44ad3e3774ecb23c6a3d0fc38176
This commit is contained in:
Dmitri Plotnikov
2010-03-24 10:20:46 -07:00
parent 3ac71b605b
commit 8f29c12d6e

View File

@ -1347,19 +1347,19 @@ public class SQLiteDatabase extends SQLiteClosable {
SQLiteCursorDriver driver = new SQLiteDirectCursorDriver(this, sql, editTable);
Cursor cursor = null;
int count = 0;
try {
cursor = driver.query(
cursorFactory != null ? cursorFactory : mFactory,
selectionArgs);
// Force query execution
if (cursor != null) {
count = cursor.getCount();
}
} finally {
if (Config.LOGV || mSlowQueryThreshold != -1) {
// Force query execution
if (cursor != null) {
cursor.moveToFirst();
cursor.moveToPosition(-1);
}
long duration = System.currentTimeMillis() - timeStart;
if (Config.LOGV || duration >= mSlowQueryThreshold) {
@ -1367,7 +1367,7 @@ public class SQLiteDatabase extends SQLiteClosable {
"query (" + duration + " ms): " + driver.toString() + ", args are "
+ (selectionArgs != null
? TextUtils.join(",", selectionArgs)
: "<null>"));
: "<null>") + ", count is " + count);
}
}
}