From 8f29c12d6ebfa3ed36e81573448fcbc49a98f514 Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Wed, 24 Mar 2010 10:20:46 -0700 Subject: [PATCH] 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 --- .../android/database/sqlite/SQLiteDatabase.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index b52f6e06d2773..3b7416e467bb1 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -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) - : "")); + : "") + ", count is " + count); } } }