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?
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] ?
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With