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:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user