From 3510268752282a47bc46c317d5dc5a3d4899edca Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 5 Sep 2020 17:47:59 -0700 Subject: [PATCH] use order-by so don't have to iterate over results This assumes, as seems to be the case, that rowid is assigned increasing order. --- .../java/org/eehouse/android/xw4/DBUtils.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java index b73ab3e8d..6ac2268c6 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java @@ -2265,22 +2265,18 @@ public class DBUtils { private static String getStringForSyncSel( SQLiteDatabase db, String selection ) { String result = null; - String[] columns = { ROW_ID, DBHelper.VALUE }; - - Cursor cursor = DBHelper.query( db, TABLE_NAMES.PAIRS, columns, selection ); - // Log.d( TAG, "getStringForSyncSel(selection=%s)", selection ); - boolean tooMany = BuildConfig.DEBUG && 1 < cursor.getCount(); - + String[] columns = { DBHelper.VALUE, }; // If there are multiple matches, we want to use the newest. At least // that's the right move where a devID's key has been changed with // each upgrade. - long prevRow = 0; - while ( cursor.moveToNext() ) { - long row = cursor.getLong(cursor.getColumnIndex(ROW_ID)); - if ( row > prevRow ) { - result = cursor.getString( cursor.getColumnIndex( DBHelper.VALUE ) ); - prevRow = row; - } + String orderBy = ROW_ID + " DESC"; + + Cursor cursor = DBHelper.query( db, TABLE_NAMES.PAIRS, columns, selection, orderBy ); + // Log.d( TAG, "getStringForSyncSel(selection=%s)", selection ); + boolean tooMany = BuildConfig.DEBUG && 1 < cursor.getCount(); + + if ( cursor.moveToNext() ) { + result = cursor.getString( cursor.getColumnIndex(DBHelper.VALUE) ); } cursor.close();