withValueBackReference的语义是什么? [英] What are the semantics of withValueBackReference?

查看:90
本文介绍了withValueBackReference的语义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清我已经使用此方法阅读了示例代码(例如,添加了新联系人的代码),并将backReference值设置为0.

I've read the example code (for example the code which adds a new contact) using this method, providing a backReference value of 0. What does this mean?

文档说:

后向引用中的列值优先于withValues(ContentValues)中指定的值

A column value from the back references takes precedence over a value specified in withValues(ContentValues)

推荐答案

此问题与内容提供商上的批处理操作有关.该示例是从此相关问题修改而成的.

This question relates to batch operation on a content provider. The example is modified from this related question.

创建一批操作时,首先创建要使用的操作列表:

When creating a batch of operations first create a list of operations to perform using:

ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

然后使用applyBatch方法将它们应用于内容提供者.

then apply them to the content provider using the applyBatch method.

ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations);

这是基本概念,因此让我们应用它.假设我们有一个内容提供商,可以处理Foo记录和一些称为Bar的子记录的uris.

That is the basic concept so let's apply it. Suppose we have a content provider which handles uris for Foo records and some child records called Bar.

content://com.stackoverflow.foobar/foo

content://com.stackoverflow.foobar/foo

content://com.stackoverflow.foobar/foo/#/bar

content://com.stackoverflow.foobar/foo/#/bar

现在,我们将只插入2条新的Foo记录,称为"Foo A".和"Foo B",这是示例.

For now we'll just insert 2 new Foo recordscalled "Foo A" and "Foo B", here's the example.

ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

//add a new ContentProviderOperation - inserting a FOO record with a name and a decscription
operations.add(ContentProviderOperation.newInsert(intent.getData())
    .withValue(FOO.NAME, "Foo A")
    .withValue(FOO.DESCRIPTION, "A foo of impeccable nature")
    .build());

//let's add another
operations.add(ContentProviderOperation.newInsert(intent.getData())
    .withValue(FOO.NAME, "Foo B")
    .withValue(FOO.DESCRIPTION, "A foo of despicable nature")
    .build());

ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations);

这里没有什么特别的,我们将2个ContentProviderOperation项目添加到我们的列表中,然后将该列表应用于我们的内容提供商.结果数组填充了我们刚刚插入的新记录的ID.

Nothing special here, we are adding 2 ContentProviderOperation items to our list and then applying the list to our content provider. The results array filled with the id's of the new records that we have just inserted.

所以说我们想做类似的事情,但我们也想通过一批操作将一些子记录添加到我们的内容提供程序中.我们希望将子记录附加到我们刚刚创建的Foo记录中.问题是我们不知道父Foo记录的ID,因为该批次尚未运行.这是withValueBackReference帮助我们的地方.让我们看一个例子:

So say we wanted to do something similar but we also want to add some child records into our content provider in one batch operation. We want to attach the child records to the Foo records we just created. The problem is we don't know the id of the parent Foo records because the batch has not been run. This is where the withValueBackReference helps us. Let's see an example:

ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

//add a new ContentProviderOperation - inserting a Foo record with a name and a decscription
operations.add(ContentProviderOperation.newInsert(intent.getData())
    .withValue(FOO.NAME, "Foo A")
    .withValue(FOO.DESCRIPTION, "Foo of impeccable nature")
    .build());

//let's add another
operations.add(ContentProviderOperation.newInsert(intent.getData())
    .withValue(FOO.NAME, "Foo B")
    .withValue(FOO.DESCRIPTION, "Foo of despicable nature")
    .build());

//now add a Bar record called [Barbarella] and relate it to [Foo A]
operations.add(ContentProviderOperation.newInsert(intent.getData()
    .buildUpon()
    .appendPath("#") /* We don't know this yet */
    .appendPath("bar")
    .build())
.withValueBackReference (BAR.FOO_ID, 0) /* Index is 0 because Foo A is the first operation in the array*/
.withValue(BAR.NAME, "Barbarella")
.withValue(BAR.GENDER, "female")
.build());

//add a Bar record called [Barbarian] and relate it to [Foo B]
operations.add(ContentProviderOperation.newInsert(intent.getData()
    .buildUpon()
    .appendPath("#") /* We don't know this yet */
    .appendPath("bar")
    .build())
.withValueBackReference (BAR.FOO_ID, 1) /* Index of parent Foo B is 1*/
.withValue(BAR.NAME, "Barbarian")
.withValue(BAR.GENDER, "male")
.build());

ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations);

所以withValueBackReference()方法允许我们在知道要与之关联的父代的ID之前插入相关记录.反向引用索引只是操作的索引,该操作将返回我们要查找的id.从您期望包含id的结果来看,可能更容易想到它.例如results[1]将包含"Foo B"的ID因此我们用来回溯引用"Foo B"的索引是1.

So the withValueBackReference() method allows us to insert the related records before we know the id of the parent we want to relate them to. The back reference index is simply the index of the operation that will return the id we want to look for. It is perhaps easier to think of it in terms of which result you would expect to contain the id. e.g results[1] would contain the id for "Foo B" so the index we use to back reference to "Foo B" is 1.

这篇关于withValueBackReference的语义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆