我想知道是否有可能使用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的索引。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐