android - withValueBackReference 的语义是什么?

标签 android android-contentprovider

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

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

文档说:

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

最佳答案

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

在创建一批操作时,首先创建一个要执行的操作列表:

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

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

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

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

content://com.stackoverflow.foobar/foo

content://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 项添加到我们的列表中,然后将列表应用到我们的内容提供者。 results 数组中填充了我们刚刚插入的新记录的 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。

关于android - withValueBackReference 的语义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4655291/

相关文章:

Android Contente 提供程序错误 : "java.lang.IllegalArgumentException: column ' theme' does not exist"

Android - contentValue 是否可以是 contentValues 数组?

java - 如何将android的textview数据添加到arraylist中

android - 如何定义网站屏幕快捷图标?

android - 为什么/我们应该在 Android 中使用 Content Provider 时实现 BaseColumns?

android - 如果在同一进程中使用 ContentProvider 有什么缺点

android - 在 Android 和 IOS 中创建像 facebook 这样的应用程序时应该使用什么样的安全性

android - 如何为从另一个 fragment 接收到的 fragment 的 TextView 设置值?

java - 执行方法之前的延迟

android - ContentProvider 抛出 SQLiteCantOpenDatabaseException : unable to open database file