0

I have a big performance issue in my app. After going through traceview i found that most of my app's performance has been consumed by cursors. So i was wondering is there any alternative to Cursors for dealing with device contact list. And if there is no alternative then please advise me how to deal with cursors so it won't slow down your app.

Please HELP!!

Thanks.

This part of my code has performance issue :-

public void getDisplayName()
{       
    Cursor c1 = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    String personName = null, number = null;
    try 
    {
        Log.e(TAG, "I am here!!");
        if(c1.getCount() > 0)
        {
            Log.e(TAG, "I am here2!!");
            while(c1.moveToNext())
            {
                HashMap<String,String> item = new HashMap<String,String>();
                String id = c1.getString(c1.getColumnIndex(Contacts._ID));
                personName = c1.getString(c1.getColumnIndex(Contacts.DISPLAY_NAME));
                item.put("Name", personName);
                Cursor cur = this.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                while(cur.moveToNext())
                {
                    Log.e(TAG, "I am here!!3");
                    number = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
                    item.put("Number", number);
                }
                displayName.add(item);          
            }
        }
    }
    finally
    {
        c1.close();
    }
}
Varundroid
  • 9,135
  • 14
  • 63
  • 93
  • As far as I know, there isn't another good way to do this. I haven't run into performance problems with Cursors, though. Maybe there's some unnecessary code in there? Post it up, and maybe somebody will spot a problem. – Geobits May 28 '11 at 13:54
  • Show us the code-passages, where you located the performance-issue. – Lukas Knuth May 28 '11 at 13:54

2 Answers2

1

I was working on same code and I need email and phone numbers both in first approach It took around 35 seconds to fetch 612 contacts, when I optimized it, it took 2 seconds to fetch 612 contacts

my code.

ContentResolver cr = context.getContentResolver();

    String[] projection = new String[] { Contacts.DISPLAY_NAME, Phone.NUMBER };

    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ?";
    String[] selectionArgs = { String.valueOf(1) };

    Cursor cur = cr.query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String phNo = cur.getString(cur.getColumnIndex(Phone.NUMBER));
            ContactModel contactModel = new ContactModel();
            contactModel.setName(name);
            contactModel.setPhoneNumber(phNo);
            contactList.add(contactModel);
        }
    }

    String[] projectionEmail = new String[] { Contacts.DISPLAY_NAME, Email.ADDRESS };

    String selectionEmail = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = ?";
    String[] selectionArgsEmail = { String.valueOf(1) };

    Cursor curEmail = cr.query(Email.CONTENT_URI, projectionEmail, selectionEmail, selectionArgsEmail, null);

    if (curEmail.getCount() > 0) {
        while (curEmail.moveToNext()) {
            String Emailname = curEmail.getString(curEmail.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String email = curEmail.getString(curEmail.getColumnIndex(Email.ADDRESS));
            ContactModel contactModel = new ContactModel();
            contactModel.setName(Emailname);
            contactModel.setEmailId(email);
            contactList.add(contactModel);
        }
    }

`

Mohd Mufiz
  • 2,236
  • 17
  • 28
1

How to fetch a contact name and number without using Cursors?

That is not possible.

I have a big performance issue in my app.

Rather than using a single query, you use N+1 queries, where N is the number of rows returned by the other query. This is guaranteed to give you poor performance compared to just doing a single query.

You can get the user's name and _ID along with the phone numbers:

    String[] PROJECTION=new String[] { Contacts._ID,
                                        Contacts.DISPLAY_NAME,
                                        Phone.NUMBER
                                        };
    Cursor c=a.managedQuery(Phone.CONTENT_URI, PROJECTION, null, null, null);

Also, never call getColumnIndex() in a loop, since the value never changes.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @CommonsWare: If i want both display_name, number + email. What is the best query? i don't know what query can get both of these. And not sure how to use cursor joiner. It's very kind if you can give me a direction. Currently, i get contact_id and create 2 cursor to look for number and email. So the performance is poor [my question] (http://stackoverflow.com/questions/10811801/effective-way-to-get-contacts-info-with-photo) – Trung Nguyen Jun 01 '12 at 07:54