主从使用 ContentResolver.applyBatch()?
我想知道是否可以在同一操作中使用 android.content.ContentResolver.applyBatch() 方法将主记录和详细记录保存到内容提供程序,其中providers 参数中的后续 ContentProviderOperation 项取决于先前项的结果。
我遇到的问题是,调用 ContentProviderOperation.newInsert(Uri) 方法时实际的 Uri 是未知的,并且 Uri 是不可变的。
我想出的如下所示:
主 Uri:content://com.foobar.masterdetail/master
详细信息 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;
}
在我的内容提供程序中,我重写了 applyBatch() 方法,以便将操作包装在事务中。
这是可能的还是有更好的方法来做到这一点?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
操作数组中的项目生成的每个结果均由其在数组中的索引标识。
后续操作可以通过 withValueBackReference() 方法引用这些结果。
可以在
此用法的完整示例">示例 ContactManager。
0 是从中获取值的 ContentProviderOperation 的索引。
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.
becomes
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.