How to keep a local DB in sync with a DB I don't own?

I'm making a RN/Expo app which is essentially a 3rd party UI for a service. Users add/edit/remove things from this remote database using the App, and the app renders that data in useful ways. At first, I was just requesting/sending data from/to the server as needed, but I would like to support an offline mode. I then made a SQLite database which is meant to stay in sync with the service when online, then when offline it will allow users to see and change data, which sync with the service later. My current way of managing this has been using React Query, where I'd request data from both the local DB and the service, combining them for display purposes and using mutations to synchronize them. However, every time I try to implement this it becomes really complex, and synchronizing via mutations in react leads to a lot of code dedicated to just making sure syncing from multiple places doesn't cause problems... I'm considering now only querying the local DB from my react code and performing the syncing behind the scenes. Logically this sounds very simple, since getting and modifying data becomes straightforward with RQ and requires no special cases for working offline. But I thought I'd ask for advice anyways since my last few attempts also seemed do-able but went poorly... What are some general strategies for keeping a local DB in sync with an online service when both of these can be updated separately? Specifically for the case where I do not have control over the online service, i.e. I'm using someone else's API to access the data.
3 Replies
Ian
Ian6mo ago
This is a very hard problem to solve ^^ First of all, what do you want to happen when there's a conflict between local & server? (Say for example, two people edit the same thing)
Remiwi
RemiwiOP6mo ago
I'd like to have it so that whenever two rows are out of sync, the most recently edited one replaces the older one, so if both are edited the more recent edit takes precedence. Luckily the service gives me this kind of info also as an update, i implemented the new solution i mentioned for one of the tables and it seems to work fine, though i still haven't finished testing it
monkijunki
monkijunki6mo ago
DM me
Want results from more Discord servers?
Add your server