android applybatch,android – 使用ContentResolver.applyBatch()的Master-detail?

我想知道是否可能在同一操作中使用android.content.ContentResolver.applyBatch()方法将内容提供程序保存到内容提供者,其中provider参数中的后续ContentProviderOperation项取决于先前项目的结果。

我遇到的问题是,当时调用了ContentProviderOperation.newInsert(Uri)方法,并且Uri是不可变的,实际的Uri是不知道的。

我想出了如下所示:

大师Uri:content://com.foobar.masterdetail/master

细节Uri:content://com.foobar.masterdetail/master/#/detail

ArrayList operations = new ArrayList();

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;

}

在我的内容提供商中,我覆盖了applyBatch()方法,以便将操作包装在事务中。

这是可能的还是有更好的方式来做到这一点?

谢谢。