views:

834

answers:

2

The goal: refresh database from XML data

The process:

  • Start transaction
  • Delete all existing rows from the tables
  • Per each main element of parsed XML insert row into main table and get PK
  • Per each child of the main element insert record into 2nd table providing FK from the previous step
  • Commit transaction

Pretty standard stuff as far as db operations. The problem is that CRUD operations are not done within ContentProvider but rather using ContentResolver so the insert for example looks like resolver.insert(CONTENT_URI, contentValues). The ContentResolver API doesn't seem to have anything pertained to transaction and I cannot use bulkInsert since I'm inserting in 2 tables intermittently (plus I want to have delete inside the transaction as well).

I was thinking of registering my customized ContentProvider as listener by using registerContentObserver but since ContentResolver#acquireProvider methods are hidden how do I obtain the right reference?

Am I out of luck?

+1  A: 

All right - so this does not dingle aimlessly: the only way I can think of is to code startTransaction and endTransaction as URL-based query requests. Something like ContentResolver.query(START_TRANSACTION, null, null, null, null). Then in ContentProvider#query based on the registered URL call start or end transaction

DroidIn.net
+1  A: 

I've seen that in the source code of Google I/O application, they override ContentProvider's applyBatch() method and use transactions inside of it. So, you create a batch of ContentProviderOperation s and then call getContentResolver().applyBatch(uri_authority, batch).

I'm planning to use this approach to see how it works. I'm curious if anyone else has tried it.

kaciula
I've tried this approach and it works well. However each ContentProviderOperation in the batch are atomic operations. What I mean by this is that there is no way to correctly handle dependent operations for master-detail relationships where the identity key created by the first operation is needed as input for subsequent operations. I've asked this before, but got zero responses (http://stackoverflow.com/questions/3224857/master-detail-using-contentresolver-applybatch).
Dan