Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Master-detail Using ContentResolver.applyBatch()?

Tags:

I was wondering if its possible to save master and detail records to a content provider using the android.content.ContentResolver.applyBatch() method in the same operation where subsequent ContentProviderOperation items in the providers parameter depend on the result of previous items.

The problem I'm having is that the actual Uri isn't known at the time that the ContentProviderOperation.newInsert(Uri) method is called and the Uri is immutable.

What I have come up with is shown below:

Master Uri: content://com.foobar.masterdetail/master
Detail Uri: content://com.foobar.masterdetail/master/#/detail

ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
operations.add(ContentProviderOperation.newInsert(intent.getData())
    .withValue(Master.NAME, "")
    .withValue(Master.VALUE, "")
    .build());
operations.add(ContentProviderOperation.newInsert(intent.getData()
        .buildUpon()
        .appendPath("#") /* ACTUAL VALUE NOT KNOWN UNTIL MASTER ROW IS SAVED */
        .appendPath("detail")
        .build())
    .withValue(Detail.MASTER_ID, /* WHAT GOES HERE? */)
    .withValue(Detail.NAME, "")
    .withValue(Detail.VALUE, "")
    .build());
ContentProviderResult[] results = this.getContentResolver().applyBatch(MasterDetail.AUTHORITY, operations);
for (ContentProviderResult result : results) {
    Uri test = result.uri;
}

In my content provider, I am overriding the applyBatch() method in order to wrap the operation in a transaction.

Is this possible or is there a better way to do this?

Thanks.

like image 208
Dan Avatar asked Jul 11 '10 22:07

Dan


People also ask

What does ContentResolver query () return?

The ContentResolver. query() client method always returns a Cursor containing the columns specified by the query's projection for the rows that match the query's selection criteria. A Cursor object provides random read access to the rows and columns it contains.

What is the use of ContentResolver in Android?

The Content Resolver behaves exactly as its name implies: it accepts requests from clients, and resolves these requests by directing them to the content provider with a distinct authority. To do this, the Content Resolver stores a mapping from authorities to Content Providers.

What is content resolver and content provider in Android?

ContentProvider and ContentResolver are part of android. content package. These two classes work together to provide robust, secure data sharing model among applications. ContentProvider exposes data stored in the SQLite database to other application without telling them the underlying implementation of your database.


1 Answers

Each result produced from an item in the operations array is identified by its index in the array. Subsequent operations may reference those results via the withValueBackReference() method.

.withValue(Detail.MASTER_ID, /* WHAT GOES HERE? */)

becomes

.withValueBackReference(Detail.MASTER_ID, 0)

A complete example of this usage can be found in sample ContactManager. The 0 is the index of the ContentProviderOperation from which the value is obtained.

like image 51
phreed Avatar answered Sep 18 '22 15:09

phreed