vendredi 31 juillet 2015

Android SQLiteDatabase SELECT query take long time

I have about 1500 records in TABLE_CONTACT. My code to get contacts:

public ArrayList<SBContact> getAllContacts() {
    SQLiteDatabase database = dbHelper.getWritableDatabase();
    ArrayList<SBContact> mContacts = new ArrayList<SBContact>();
    String selectQuery = "SELECT  * FROM " + SBDatabaseHelper.TABLE_CONTACT;
    Cursor cursor = database.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            SBContact result = new SBContact(cursor.getString(cursor.getColumnIndex(SBDatabaseHelper.CONTACT_NUMBER)),
                    cursor.getString(cursor.getColumnIndex(SBDatabaseHelper.CONTACT_NAME)), cursor.getInt(cursor.getColumnIndex(SBDatabaseHelper.EXITS_USER)));
            mContacts.add(result);
        } while (cursor.moveToNext());
    }

    return mContacts;
}

The problem is: The first time a run this method, it take about 15ms. If 5 seconds later, I run this method again, it take about 20 seconds.

In similar way, after the first call, 15s later, run method again, it take about 10 seconds. And after 2 minutes later, run this method again, it take about 15ms as the first time run.

In this time, no other thread make a read/write query to database.

I don't understand what the SQLiteDatabase do. Does it need time to release memory or do something?

Aucun commentaire:

Enregistrer un commentaire