I realize this has been somewhat touched upon in various places including here on Stack Overflow, but I’m looking for any other solutions that people might have used. So with that in mind…
I’m developing an application where a user can initially sync all his contacts with a desktop application OTA. This is done through a web service call that grabs a set of 100 contacts from the server, downloads and parses the information, inserts the contacts into the Android Contact DB, acknowledges receipt of these contacts, and then repeats the previous steps with the next set of 100 contacts until the sync is complete. This process works well when a user has contacts on the order or 1000-2000, but a typical user of this application can easily have 5000-6000 contacts (with power users having upwards of 10000+) in which case things take far longer than I’d like. For example, a sample set of approximately 5300 contacts can take about 13.5 minutes to complete. Not bad, but I’d like it to be at least as efficient as iOS which runs about 8 minutes for the same data set if possible.
I’ve logged the time it takes for each step and, unsurprisingly, the bottleneck appears to be with inserting the data into the Android contract DB. After scouring the web I’ve found little help with regards to inserting thousands of contacts, but what I have found seems to fall into these three groups:
1) ContentProviderOperation — The Google recommended way which gave me my baseline of 13.5 minutes for 5300 contacts.
2) Bulk Inserts — I read that builkInsert tends to be more efficient than applyBatch, but when I tried to implement this myself it actually took 25 minutes for the same 5300 contacts. I have a feeling a lot of this is due to the fact that I need to insert the RawContact information and then save the resulting URI for use in creating the ContactsContract.Data for the bulkInsert which comes more naturally via the backValueReference in the ContentProviderOperation. Additionally, I looked at the android source code and I don’t get the feeling that bulkInsert is terribly efficient.
3) Creating an optimized bulk insert using the DatabaseUtils.InsertHelper and transactions — Unfortunately, this seems geared towards those people who created their own content provider because you need access to the underlying DB as an instance variable and I’ve yet to see how that could be done with the native contacts DB.
Does anyone have any experience with inserting 5000+ contacts or any other possible ideas I could look into to help reduce my time? Or should the ContentProviderOperation be considered as optimized as it’s going to get?
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.