Elad Gelman
Elad Gelman

Reputation: 1182

android Contact list performance

I have performance Issues when trying to load a list of contacts. this is the code:

public ContactList getContacts(String constraint)
{
    ContactList contactList = new ContactList();

    Uri uri = ContactsContract.Contacts.CONTENT_URI;

    if (constraint != null && constraint.equals("") == false)
    {
        uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_FILTER_URI, constraint);
    }

    ContentResolver cr = _context.getContentResolver();
    String sortOrder = DISPLAY_NAME + " COLLATE LOCALIZED ASC";
    // Cursor cur = cr.query(uri, null, null, null, sortOrder);
    String[] arrayOfString = new String[1];
    arrayOfString[0] = "1";
    Cursor cur = cr.query(uri, null, "has_phone_number=?", arrayOfString,
            sortOrder);

    if (cur.getCount() > 0)
    {

        setBaseContactList(contactList, cur);

    }
    cur.close();

    return contactList;
}

private void setBaseContactList(ContactList contactList, Cursor cur)
{
    String id;
    String name;
    String lookUpKey;
    String photoId;
    while (cur.moveToNext())
    {
        id = cur.getString(cur.getColumnIndex(DISPLAY_ID));
        name = cur.getString(cur.getColumnIndex(DISPLAY_NAME));

        photoId = cur.getString(cur.getColumnIndex(PHOTO_ID));
        lookUpKey = cur.getString(cur.getColumnIndex(LOOKUP_KEY));
        Contact c = new Contact(id, name, photoId, lookUpKey);
        contactList.addContact(c);
    }
}

looking at traceview I can see that cur.getString(cur.getColumnIndex("Some column name")); Is taking it's toll on the loading time

another thing that I can see while using traceview is that there are alot of callings to the method

String.equalsIgnoreCase()

my guess is that cur.getString is calling it.

Im thinking about putting all the values that are initiated onCreate() in an HT(ContactID, Contact) and then calling them after every selection from the contacts db using the user ids that were extracted. I will cut down the usage 3 times which is prbbly good but is there a better solution?

Thanks

Upvotes: 0

Views: 353

Answers (2)

amirlazarovich
amirlazarovich

Reputation: 906

On top of what @wildhemp suggested i would also try to query for specific columns as well and not pass "null" for all columns. I would also check how much cycles would be saved if i fetched values from hard-coded indexes (not best practice, but might speed the process a bit).

For example:

Cursor cur = cr.query(uri, 
                      new String[] {DISPLAY_ID, DISPLAY_NAME, PHOTO_ID, LOOKUP_KEY},
                      "has_phone_number=?", arrayOfString, sortOrder);
while (cur.moveToNext())
{
    id = cur.getString(0);
    name = cur.getString(1);
    photoId = cur.getString(2);
    lookUpKey = cur.getString(3);

    Contact c = new Contact(id, name, photoId, lookUpKey);
    contactList.addContact(c);
}

Another thing i would check is querying without any sorting, it is interesting to see how much extra cycles it uses for sorting, maybe there's another, more efficient, way to do it.

Although this is quite obvious i would still ask whether the Customer class is lightweight and if it does nothing but initializes its members. Also, that ContactList's addContact method simply adds another item to its wrapped or extended List<Customer>.

General note, it is always good practice to surround any cursor related work with try/catch/finally to ensure that you close the cursor when you're done.

Anyway, when dealing with issues such as this one it is either the query is not efficient or the parsing loop is doing something wrong

Upvotes: 2

wildhemp
wildhemp

Reputation: 326

I would say the equalsIgnoreCase is called from cur.getColumnIndex. You could make it faster if instead of calling those every time in the while cycle you would call them only once and store them.

Upvotes: 1

Related Questions