Ideally, I'd like to use ContentResolver.applyBatch() to insert a large number of contacts. This provides the needed transaction support to guarantee to the user that the entire batch either was completely inserted or not at all. It also provides the "back reference" linking of FKs to PKs. Unfortunately there is a limit of 500 operations per batch, which is not large enough for my usage.
Besides simply breaking the inserts into smaller batches, which doesn't provide a fail safe way to do the overall operation, I could use BulkInsert(). Is there a fail-safe way to use this? Here's what I'm thinking: First, the scope is to insert a bunch of RawContact rows and, for each, several Data rows. 1. Do a BulkInsert() for all the RawContact rows: For each row, set SOURCE_ID to com.mydomain.<my source id>, where <my source id> is a persistent index to the data I'm reading to do the inserts. If the action doesn't complete, I'm presuming upon restart I can do a single delete: where SOURCE_ID like 'com.mydomain.%'. 2. Query all the rows I just inserted: Use: where SOURCE_ID like 'com.mydomain.%'. From this I can get the new row _ID values for each of the rows I just inserted. I'll need these ids for the next step. 3. Do a BulkInsert() for all the Data rows: Create Data rows with an FK determined from step 2 - the RawContact._ID. If this fails before completing, upon restart, I can delete my new rows similarly to what I described for step 1. 4. Clear SOURCE_ID: The SOURCE_ID is for the sync adapter. According to the android documentation <https://developer.android.com/guide/topics/providers/contacts-provider.html>this should be left blank to indicate that the sync adapter must create a new contact on its server. I presume that while not null the sync adapter will ignore the row and when I set it to NULL the sync adapter will be able to do its work. I see two potential problems with this. Should I be concerned? Is there a better way? A. If there is a failure, the contacts database will be left in a bad state until my app is restarted and can clear out the partially created rows. B. By putting my own value in SOURCE_ID, I am assuming the sync adapter will ignore the row. However, it might try to update rows on its server using the bogus SOURCE_ID values. Clearly those updates will fail. Will the sync adapter handle the failures nicely? I would very pleased to hear any comments and suggestions on this dilemma. Thanks. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/8bf56843-29d3-48bc-ae4f-e8b460dd0a7f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.