Insertion of Thousands of Contact Entries Using Applybatch Is Slow

Bulk update of more than 500 contacts

You're not creating a new object for ops. During subsequent calls to applyBatch, you're passing the previously applied operations back in as well. The first time ops contains 100 elements, then 200 and eventually it fails when it reaches 500. Change to

if (i % batchsize == 0) {
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
ops = new ArrayList<ContentProviderOperation>(100);
}

on contact insert get Contact_ID

Better way, and you get the _ID not the RAW_ID!!

Add that as static variables:

static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.TIMES_CONTACTED,
ContactsContract.Contacts.CONTACT_PRESENCE,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};

Then add that after the insert:

// Displayed name
String displayed_name = "Contact's name goes here";

// Add the new contact
try
{
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}

String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +displayed_name+ "\" )";
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
context.startManagingCursor(c);

if (c.moveToNext())
{
int id = new Integer(c.getString(0)).intValue();
}

It works for me.

Multiple rows insert with ContentProvider

On the client side, ContentResolver supports a bulkInsert() method. Those will not necessarily be processed in a single transaction by the ContentProvider, simply because there may not be any transactions performed by the ContentProvider.

Most Efficient Way to Insert 5000+ Android Contacts

Unfortunately, I believe 1 is the best option. I suspect a majority of your overhead in comparison to iPhone is in the cross process IPC inherent to the content provider design.

Your analysis of 3 is correct.

There are options on rooted devices to go around the content provider but I doubt that is what you are looking for.



Related Topics



Leave a reply



Submit