Bulk update over 500 contacts

I am developing an application that should update many contacts, and I get the following error.

android.content.OperationApplicationException: Too many content provider operations between yield points. The maximum number of operations per current point is 500

I tried to break the contacts into smaller pieces for updating, but I still get the same error. It’s good that now some contacts are updated (previously 0 contacts are updated). Any suggestions that might help me are greatly appreciated.

Uri uri = ContactsContract.Data.CONTENT_URI; String selectionUpdate = ContactsContract.CommonDataKinds.Phone._ID + " = ? AND " + ContactsContract.Contacts.Data.MIMETYPE + " = ? "; int i = 0; int numRowsUpdated = 0; int batchsize = 100; for (EntityPhone ep : eps) { if (ep.isUpdateNumber()) { //update only when checkbox is ticked ops.add(ContentProviderOperation.newUpdate(uri) .withSelection(selectionUpdate, new String[]{ep.getPhoneId(), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, ep.getPhoneNumberNew()) .build()); i++; if (i % batchsize == 0) { i = 0; ContentProviderResult[] count = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); if (count != null) { numRowsUpdated += count.length; Log.i(TAG, "batch update success" + count.length); } else { Log.w(TAG, "batch update failed"); } } } } if (i != 0) { ContentProviderResult[] count = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops); } 

I looked through past questions, but they are mostly related to inserts, not updates.

The reason I want to update so many records at once is that my application is a β€œcontact number formatter,” which allows the user to easily standardize all the phone numbers on the phone. I can not control the number of records that users want to update in one batch. ( https://play.google.com/store/apps/details?id=angel.phoneformat )

+6
source share
1 answer

You are not creating a new object for ops . During subsequent calls to applyBatch you also return previously applied operations. For the first time, ops contains 100 items, then 200, and finally crashes when it reaches 500. Go to

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

All Articles