ContactResolver.applyBatch (…) иногда возвращает пустой результат

Я добавляю jsonned контакты в свое Android-устройство ilke следующим образом (обратите внимание, что приведенный ниже код является черновиком, то есть может содержать некоторое неэффективное содержимое);

... if(!is_duplicated) { String firstName = p_contact.getContactFirstName(); String middleName = p_contact.getContactMiddleName(); String lastName = p_contact.getContactLastName(); String orgName = p_contact.getOrganizationName(); boolean isFirstNameNull = (firstName != null && firstName.equalsIgnoreCase("null")); boolean isMiddleNameNull = (middleName != null && middleName.equalsIgnoreCase("null")); boolean isLastNameNull = (lastName != null && lastName.equalsIgnoreCase("null")); boolean isOrgNameNull = (orgName != null && orgName.equalsIgnoreCase("null")); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) .build()); //----------------- Names if(!isFirstNameNull && !isMiddleNameNull && !isLastNameNull) // 111 { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName) .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName) .build()); } else if(isFirstNameNull && !isMiddleNameNull && !isLastNameNull) // 011 { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName) .build()); } else if(!isFirstNameNull && isMiddleNameNull && !isLastNameNull) // 101 { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName) .build()); } else if(!isFirstNameNull && !isMiddleNameNull && isLastNameNull) // 110 { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName) .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName) .build()); } else if(isFirstNameNull && isMiddleNameNull && !isLastNameNull) // 001 { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName) .build()); } else if(isFirstNameNull && !isMiddleNameNull && isLastNameNull) // 010 { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName) .build()); } else if(!isFirstNameNull && isMiddleNameNull && isLastNameNull) // 100 { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName) .build()); } else if(isFirstNameNull && isMiddleNameNull && isLastNameNull) // 000 { /*ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Anonim") .build());*/ } if(!isOrgNameNull) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, pIndex) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, orgName) .build()); } Iterator<TreeMap.Entry<String, String>> entries = p_contact.getPhonesHashMap().entrySet().iterator(); while (entries.hasNext()) { TreeMap.Entry<String, String> entry = entries.next(); String type = entry.getKey(); String number = entry.getValue(); if(type.equalsIgnoreCase("home")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME) .build()); } else if(type.equalsIgnoreCase("work")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK) .build()); } else if(type.equalsIgnoreCase("mobile")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) .build()); } else if(type.equalsIgnoreCase("faxhome")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME) .build()); } ... } Iterator<TreeMap.Entry<String, String>> email_entries = p_contact.getEmailsHashMap().entrySet().iterator(); while (email_entries.hasNext()) { TreeMap.Entry<String, String> entry = email_entries.next(); String type = entry.getKey(); String address = entry.getValue(); if(firstName != null && firstName.equals("Yasin")) { Log.d("ContactIssueHandler", "email is: " + address); Log.d("ContactIssueHandler", "email type is: " + type); } if(type.equalsIgnoreCase("home")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.DATA, address) .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME) .build()); } else if(type.equalsIgnoreCase("work")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address) .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK) .build()); } else if(type.equalsIgnoreCase("mobile")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address) .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM) .build()); } else if(type.equalsIgnoreCase("other")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address) .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_OTHER) .build()); } else { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address) .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM) .withValue(ContactsContract.CommonDataKinds.Email.LABEL, type) .build()); } } // Asking the Contact provider to create a new contact try { p_contact.setIsAdded(true); p_contact.setReasonForFail(REASON_FOR_FAIL.NO_FAIL); ContentProviderResult[] results = p_application_context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); Log.d("ContactIssueHandler", "result: " + results.length); Log.d("ContactIssueHandler", "opssize: " + ops.size()); if(results.length != ops.size()) { p_contact.setIsAdded(false); p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE); //or throw exp. } } catch (OperationApplicationException oaexp) { oaexp.printStackTrace(); p_contact.setIsAdded(false); p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE); } catch (RemoteException rexp) { rexp.printStackTrace(); p_contact.setIsAdded(false); p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE); } catch (Exception e) { e.printStackTrace(); p_contact.setIsAdded(false); p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE); } ... 

В любом случае. Каждый раз, когда я добавляю jsonned-контакт, мне нужно выполнить вышеуказанный код. Интересно то, что когда я вставляю, например, 50 контактов, всегда некоторые конкретные контакты не вставлены, потому что ContentResolverResult возвращается пустым массивом методом applyBatch (не из-за исключения исключения). Но если я попытаюсь добавить один и тот же «не добавил» контакты один за другим, они добавляются!

Так что может быть проблемой?