从 ContentProviderResult[] 中删除失败还是成功?
使用 contentResolver.delete(uri, null, null)
时,可以通过查看受影响的行数的返回值来确定是否成功。
但是,通过 ContentProviderOperation
和 applyBatch
删除联系人> 返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将检查结果的计数字段,看看它是否等于 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.
根据 Content Resolver 类中的 applyBatch 方法的文档,如果任何操作失败,applyBatch 不会抛出错误吗?
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?
由于您要删除一个联系人,因此返回的数组结果应该具有相同的长度。这将验证操作成功/失败
Since you are deleting one contacting so the result of array returned should be of same length. This will validate the operation success/fail