从 ContentProviderResult[] 中删除失败还是成功?

发布于 2024-12-12 15:07:13 字数 954 浏览 1 评论 0原文

使用 contentResolver.delete(uri, null, null) 时,可以通过查看受影响的行数的返回值来确定是否成功。

但是,通过 ContentProviderOperationapplyBatch 删除联系人> 返回 ContentProviderResult[]

如何从 ContentProviderResult 对象中识别删除操作是否成功?

ContentProviderOperation 删除失败吗?

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

ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
      .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)})
      .build());

ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if (results != null && results[0] != null) {                            
    // How to extract whether success or failure from results[0] ?
}

When using contentResolver.delete(uri, null, null) then determining success can be done by looking at the return value of how many rows were affected.

However, deleting a contact through ContentProviderOperation and applyBatch returns ContentProviderResult[]

How can you identify whether the delete operation was successful or not from the ContentProviderResult object?

Did delete fail with ContentProviderOperation ?

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

ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
      .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)})
      .build());

ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if (results != null && results[0] != null) {                            
    // How to extract whether success or failure from results[0] ?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤单情人 2024-12-19 15:07:14

您将检查结果的计数字段,看看它是否等于 1。如果您运行问题中的操作两次,第一个结果应该为您提供计数 1(表示删除了一行),而第二个结果应该为您提供计数。应该给你一个 0 的计数(表明没有行被删除,因为你已经删除了它)。

事实上,操作并没有失败(因此也不例外)。第二次查询就没有效果了。

You would check the count field of the Result and see if it is equal to 1. If you ran the operation in your question twice, the first Result should give you a count of 1 (indicating one row was deleted), while the second Result should give you a count of 0 (indicating no rows were deleted since you already deleted it).

The truth is the operation isn't failing (hence no exception). The query just has no effect the second time around.

风透绣罗衣 2024-12-19 15:07:14

根据 Content Resolver 类中的 applyBatch 方法的文档,如果任何操作失败,applyBatch 不会抛出错误吗?

如果应用程序失败,则抛出OperationApplicationException。请参阅 apply(ContentProvider, ContentProviderResult[], int) 了解更多信息。

Per the documentation for the applyBatch method from the Content Resolver class, won't the applyBatch throw an error if any of the operations failed?

OperationApplicationException thrown if an application fails. See apply(ContentProvider, ContentProviderResult[], int) for more information.

对岸观火 2024-12-19 15:07:14

由于您要删除一个联系人,因此返回的数组结果应该具有相同的长度。这将验证操作成功/失败

Since you are deleting one contacting so the result of array returned should be of same length. This will validate the operation success/fail

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文