Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete fail or success from ContentProviderResult[]?

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] ?
}
like image 418
Cel Avatar asked Oct 28 '11 16:10

Cel


1 Answers

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.

like image 198
Justin Breitfeller Avatar answered Oct 20 '22 09:10

Justin Breitfeller