I currently have a windows app I wrote that interacts with data from a web API. I'm trying to make an android version to carry around with me. Generally only one person accesses the data from their account, but from multiple devices. I tried to go local-first, by updating the local RoomDB first and then creating a worker to update the API as connection was available.

The problem I'm running into is the API needs to be the single source of truth. It assigns Id's to the records. I want the app to work offline, but I'm stumped on a good strategy to keep everything in sync. For the sake of argument, let's just call it a simple todo app. I want to be able to do simple CRUD operations from my phone, and be notified when they're due.

I've searched all over on this and nothing seems to fit. Any ideas or resources i might have missed?

2 Replies 2

so what is wrong with what you were doing with the RoomDB? That is a normal strategy for syncing content, just leave the server id field blank locally until the server can assign an ID

There's a few synch issues you can see here:

1)Creation issues reusing the same id. The solution here is not to use numeric rising ids, but to use a random UUID for each item. Thus the chance of reusing an ID is less than your chance of winning the lottery, and you can just ignore it.

Another way to do this is to have two ids, a server and a local id. This avoids the tiny chance of a uuid collision, but generally isn't worth the extra complexity.

2)Editing sync issues- if the same item is edited on multiple devices at roughly the same time. There's a lot of strategies for this, and the right one depends on the application. We don't have enough info to help here. But in general the most used strategy is last write wins (basically saying whoever syncs last is what gets remembered), just because it's the default if you don't code to prevent it.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.