主从使用 ContentResolver.applyBatch()?

发布于 2024-09-09 04:29:14 字数 1304 浏览 1 评论 0原文

我想知道是否可以在同一操作中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2024-09-16 04:29:14

操作数组中的项目生成的每个结果均由其在数组中的索引标识。
后续操作可以通过 withValueBackReference() 方法引用这些结果。

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

可以在

.withValueBackReference(Detail.MASTER_ID, 0)

此用法的完整示例">示例 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.

.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文