Cel
Cel

Reputation: 6669

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] ?
}

Upvotes: 7

Views: 2758

Answers (3)

Justin Breitfeller
Justin Breitfeller

Reputation: 13801

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.

Upvotes: 5

Saurabh
Saurabh

Reputation: 7964

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

Upvotes: 0

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30845

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.

Upvotes: 0

Related Questions