vendredi 5 juin 2015

ContentProvider URI to update multiple records in a single transaction

I am using a RecyclerView to implement drag and drop functionality, "reordering of the cells". So each row has an _id and _caption and a _order. The _order field in the database table determines the sorting order.

When sorting changes, then basically the two rows in question swap their sorting order values. eg.

ROW1= 1001, "a Caption for Row1", 1
ROW2= 1002, "a Caption for Row2", 2

When Row2 to is draged over Row1 the table entries will change to be like this:

ROW1= 1002, "a Caption for Row2", 1
ROW2= 1001, "a Caption for Row1", 2

However when I move cells "rapidly" I end up with multiple rows having the same sorting order value.

I use two SQL queries in a ContentProviderOperation[] to change the ordering.

I have tried the code below in my ContentProvider:

   @Override
    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
        ContentProviderResult[] results = null;
        try{
            database.beginTransaction();
            results = super.applyBatch(operations);
            database.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
        }
        return results;
    }

Does this make the two transaction to be executed atomically?

And how else can I fix this problem.... perhaps a URI that looks like this:

/#ROW_ID_1/#TO_ORDER_VALUE_1/#ROW_ID_2/#TO_ORDER_VALUE_2

Then again I am unsure of how to perform the SQL statement in a single transaction for SQL-LITE.

Aucun commentaire:

Enregistrer un commentaire