Android withValueBackReference的语义是什么?

Android withValueBackReference的语义是什么?,android,android-contentprovider,Android,Android Contentprovider,我搞不懂这个词的确切语义 我已经使用此方法阅读了示例代码(例如添加新联系人的代码),提供了0的backReference值。这是什么意思 文件说: 后面引用的列值优先于withValues(ContentValues)中指定的值 此问题与内容提供商上的批处理操作有关。该示例由修改而来 创建批处理操作时,首先创建要执行的操作列表,使用: ArrayList<ContentProviderOperation> operations = new ArrayList<ContentPr

我搞不懂这个词的确切语义

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

文件说:

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


此问题与内容提供商上的批处理操作有关。该示例由修改而来

创建批处理操作时,首先创建要执行的操作列表,使用:

ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
这是基本概念,让我们应用它。假设我们有一个内容提供者,它处理Foo记录和一些名为Bar的子记录的uri

content://com.stackoverflow.foobar/foo

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

现在,我们只插入两个新的Foo记录scaled“fooa”和“foob”,下面是示例

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);
ArrayList operations=new ArrayList();
//添加新的ContentProviderOperation-插入具有名称和声明的FOO记录
add(ContentProviderOperation.newInsert(intent.getData())
.withValue(FOO.NAME,“FOO A”)
.withValue(FOO.DESCRIPTION,“性质无可挑剔的FOO”)
.build());
//让我们再加一个
add(ContentProviderOperation.newInsert(intent.getData())
.withValue(FOO.NAME,“FOO B”)
.withValue(FOO.DESCRIPTION,“性质卑鄙的FOO”)
.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);
ArrayList operations=new ArrayList();
//添加新的ContentProviderOperation-插入具有名称和声明的Foo记录
add(ContentProviderOperation.newInsert(intent.getData())
.withValue(FOO.NAME,“FOO A”)
.withValue(FOO.DESCRIPTION,“性质无可挑剔的FOO”)
.build());
//让我们再加一个
add(ContentProviderOperation.newInsert(intent.getData())
.withValue(FOO.NAME,“FOO B”)
.有价值(FOO.DESCRIPTION,“性质卑鄙的FOO”)
.build());
//现在添加一个名为[Barbarella]的酒吧记录,并将其与[Foo a]关联
add(ContentProviderOperation.newInsert(intent.getData())
.buildon()
.appendPath(“#”)/*我们还不知道这一点*/
.appendPath(“bar”)
.build())
.withValueBackReference(BAR.FOO_ID,0)/*索引为0,因为FOO A是数组中的第一个操作*/
.withValue(BAR.NAME,“Barbarella”)
.withValue(BAR.性别,“女性”)
.build());
//添加名为[Barbarian]的酒吧记录并将其与[Foo B]关联
add(ContentProviderOperation.newInsert(intent.getData())
.buildon()
.appendPath(“#”)/*我们还不知道这一点*/
.appendPath(“bar”)
.build())
.withValueBackReference(BAR.FOO_ID,1)/*父FOO B的索引为1*/
.withValue(BAR.NAME,“野蛮人”)
.withValue(BAR.性别,“男性”)
.build());
ContentProviderResult[]results=this.getContentResolver().applyBatch(FooBar.AUTHORITY,operations);

因此,withValueBackReference()方法允许我们在知道要关联的父记录的id之前插入相关记录。back引用索引只是返回我们要查找的id的操作的索引。也许更容易根据您希望包含id的结果来考虑它。例如,
results[1]
将包含“Foo B”的id,因此我们用于返回“Foo B”引用的索引是1。

我在SO上找到了这个讨论-它讨论了在目标是在单个操作中保存主记录和详细记录的情况下使用withValueBackReference方法。不过,我还是不明白这里0位数的背面参考值是怎么回事!这是一个无可挑剔的解释。非常感谢!我多么希望我能不止一次投票。这是gold,我见过的唯一一个关于反向引用的体面解释。在我的应用程序中实现了它-谢谢有史以来最好的答案,非常感谢,如果有一个星旗,你只能放在5个响应中,那就太好了:)你肯定拥有一个!您确定要更换
#
登录
content://com.stackoverflow.foobar/foo/#/bar
主记录的id是否有效?我试过了,但似乎没有。如果我仔细看一下,似乎反引用只应用于
ContentValues
,而不是URI。非常好的解释,让我意识到了一个让我省去很多时间痛苦的特性。谢谢。
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);