Insertion of thousands of contact entries using applyBatch is slow

后端 未结 7 1993
我在风中等你
我在风中等你 2020-11-28 23:02

I\'m developing an application where I need to insert lots of Contact entries. At the current time approx 600 contacts with a total of 6000 phone numbers. The biggest contac

相关标签:
7条回答
  • 2020-11-28 23:35

    Here is am example of inserting same data amount within 30 seconds.

     public void testBatchInsertion() throws RemoteException, OperationApplicationException {
        final SimpleDateFormat FORMATTER = new SimpleDateFormat("mm:ss.SSS");
        long startTime = System.currentTimeMillis();
        Log.d("BatchInsertionTest", "Starting batch insertion on: " + new Date(startTime));
    
        final int MAX_OPERATIONS_FOR_INSERTION = 200;
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();
        for(int i = 0; i < 600; i++){
            generateSampleProviderOperation(ops);
            if(ops.size() >= MAX_OPERATIONS_FOR_INSERTION){
                getContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
                ops.clear();
            }
        }
        if(ops.size() > 0)
            getContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
        Log.d("BatchInsertionTest", "End of batch insertion, elapsed: " + FORMATTER.format(new Date(System.currentTimeMillis() - startTime)));
    
    }
    private void generateSampleProviderOperation(ArrayList<ContentProviderOperation> ops){
        int backReference = ops.size();
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED)
                .build()
        );
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backReference)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "GIVEN_NAME " + (backReference + 1))
                        .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "FAMILY_NAME")
                        .build()
        );
        for(int i = 0; i < 10; i++)
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backReference)
                            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MAIN)
                            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, Integer.toString((backReference + 1) * 10 + i))
                            .build()
            );
    }
    

    The log: 02-17 12:48:45.496 2073-2090/com.vayosoft.mlab D/BatchInsertionTest﹕ Starting batch insertion on: Wed Feb 17 12:48:45 GMT+02:00 2016 02-17 12:49:16.446 2073-2090/com.vayosoft.mlab D/BatchInsertionTest﹕ End of batch insertion, elapsed: 00:30.951

    0 讨论(0)
提交回复
热议问题