withValueBackReference的语义?语义、withValueBackReference

2023-09-12 03:50:00 作者:往事随风

我已阅读文档多次,但我仍然想不通的withValueBackReference.

I have read the docs multiple times, but I still cannot figure out the exact semantics of withValueBackReference.

我看到的一些例子code(例如code增加了一个新的联系人)使用这种方法,提供0反向引用值,这是什么意思?

I see some of 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?

该文档有这样一段话:

从后面引用的列值将precedence过的withValues​​(ContentValues​​)指定的值

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

任何帮助是极大的AP preciated。

Any help is greatly appreciated.

推荐答案

这个问题涉及到批量操作的内容提供商。这个例子是从此相关的问题。

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);

这是基本的概念,所以让我们来应用它。假设我们有它处理的URI美孚记录和一些所谓的酒吧子记录内容提供商。

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.

内容://com.stackoverflow.foobar/foo

content://com.stackoverflow.foobar/foo

内容://com.stackoverflow.foobar/foo/#/bar

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

现在我们只需插入2个新富recordscalled富A和富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项目我们的名单,然后将名单给我们的内容提供商。结果数组充满了身份证的,我们刚刚插入的新记录。

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.

所以说,我们想要做类似的事情,但我们也想了一些子记录添加到我们的内容提供商在一个批处理操作。我们希望给孩子记录连接到我们刚刚创建的富记录。问题是我们不知道的母公司富记录的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。例如,结果[1] 将包含ID为富B,所以我们用它来反向引用索引富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.