Delete failure or success from ContentProviderResult []?

When using contentResolver.delete(uri, null, null) then a success determination can be made by looking at the return value of the number of rows.

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

How can you determine if the delete operation was successful or not from the ContentProviderResult object?

Did uninstall succeed 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] ? } 
+6
source share
3 answers

You would check the result counter field and see if it is 1. If you ran the operation twice in your question, the first result should give you a score of 1 (indicating that one row was deleted), while the second result should give you a score of 0 (this means that no row has been deleted since you already deleted it).

The truth is that the operation does not fail (hence the exception). The request simply does not work the second time.

+4
source

In the documentation for the applyBatch method from the Content Resolver class, will applyBatch not throw an error if any of the operations fail?

OperationApplicationException if the application does not work. For more information, see Application (ContentProvider, ContentProviderResult [], int).

0
source

Since you delete one contact, therefore, the result of the returned array should have the same length. This will confirm the success / failure of the operation.

0
source

Source: https://habr.com/ru/post/900299/


All Articles