我想知道是否有可能使用android.content.ContentResolver.applyBatch()方法将主logging和详细logging保存到内容提供者中,而提供者参数中随后的ContentProviderOperation项目依赖于之前项目的结果。
我遇到的问题是实际的Uri在调用ContentProviderOperation.newInsert(Uri)方法时不知道,Uri是不可变的。
我已经拿出了如下:
Uri大师:内容://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()方法以便在事务中包装操作。
这是可能的还是有更好的方法来做到这一点?
谢谢。
操作数组中的一个项目产生的每个结果都由它在数组中的索引来标识。 后续操作可以通过withValueBackReference()方法引用这些结果。
.withValue(Detail.MASTER_ID, /* WHAT GOES HERE? */)
变
.withValueBackReference(Detail.MASTER_ID, 0)
这个用法的完整例子可以在示例ContactManager中find。 0是从中获取值的ContentProviderOperation的索引。