I am developing a RESTful Android app using SyncAdapter. I have watched some screencasts and read some tutorials, but they only address basic dynamics. Before starting to write the code, I would like to try and receive feedback from expert users about concurrency issues.
Step 1. The user U
inserts a new entry E
into the table T
of the database. The entry has a column status = TO_SYNC
.
Step 2. Before the entry E
is synced, U
decides to modify it. An activity A
starts for modifying the column values of E
.
Step 3. While the user is modifying the entry, the SyncAdapter starts and sends the entries with status == TO_SYNC
to the server. For each entry, the SyncAdapter
sets status = SYNCED
once it receives a positive answer from the server.
Step 4. Let's say that a concurrent access to the same entry E
causes a conflict:
- The SyncAdapter reads
E
, sendsE
to the server - The activity
A
completes and setsE
status toTO_SYNC
- The SyncAdapter receives the ok from the server and sets the
status
ofE
toSYNCED
- At this point the new values of
E
will not be synced since the valueTO_SYNC
has been overwritten by the SyncAdapter.
My question is: how can I avoid such issue without blocking the entire database with a begin/end transaction
while syncing (which could take a long time to complete)? Should I fall back on a classical java lock on single entries? Is there a more elegant way?