mardi 1 mars 2016

Android - How to user transactions with modifying database via ContentProvider?

When a user taps their NFC tag, my app:

  1. Inserts a new user into the users table, getting back the user _id, then
  2. Writes this _id value to the user's NFC tag.

The problem is that if that user moves their tag away too quickly, step 2 will fail with an IOException. The user will then have to tap their tag again, but this will result in step 1 being repeated - i.e., duplicate entries. :-/

To overcome this issue, I would like to use transactions based on this example code from the API documentation:

   db.beginTransaction();
   try {
     // Insert data into database, getting back _id
     // Write _id to NFC tag
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }

...but my database is wrapped in a MyContentProvider, so I don't have access to db.

Is there a way to use transactions when inserting data without abandoning the usage of the MyContentProvider?

Aucun commentaire:

Enregistrer un commentaire