withValueBackReference的语义是什么?

时间:2011-01-11 07:44:13

标签: android android-contentprovider

我无法弄清楚withValueBackReference的确切语义。

我已经使用此方法阅读了示例代码(例如添加新联系人的代码),提供的backReference值为0.这是什么意思?

文档说:

  

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

1 个答案:

答案 0 :(得分:160)

此问题与内容提供商的批处理操作有关。该示例已从this related question修改。

创建一批操作时,首先使用以下命令创建要执行的操作列表:

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

然后使用applyBatch方法将它们应用于内容提供程序。

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

这是基本概念,所以让我们应用它。假设我们有一个内容提供程序来处理用于Foo记录的uris和一些名为Bar的子记录。

  

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

     

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

现在我们只插入2个新的Foo唱片,名为“Foo A”和“Foo B”,这是一个例子。

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。

所以说我们想做类似的事情,但我们也希望在一次批量操作中将一些子记录添加到我们的内容提供者中。我们希望将子记录附加到我们刚刚创建的Foo记录中。问题是我们不知道父Foo记录的id,因为批处理尚未运行。这是withValueBackReference帮助我们的地方。我们来看一个例子:

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.

相关问题