This is a very tricky yet a rather common mobile development challenge. No silver bullet here and plenty of ways to get it wrong.
Now if you're doing this for an iOS project I'd recommend using Core Data or any local store you are comfortable with. You will need to decide on two things.
1- Your source of truth: basically the master database, in case of conflicts (i.e. you are updating a piece of data that has already been deleted from the master database two hours ago) master should win. Typically the master database is the remote one.
2- Sync frequency - will you attempt to sync anytime new data is created/updated on the local database or on a specific schedule.
Example - Actual sync strategy for a production mobile app :
local database is synced to the remote source every 30-90 seconds
A local journal of pending updates needing to be synced is maintained. An ordered set of update logs, stored locally alongside the actual data - basically metadata. (optional but highly useful)
The journal can also be useful in applying updates to the remote source in the right order or removing
pointless update requests - i.e. you rename a document then you delete the same document five minutes later - locally . The remote database only needs the final event, not the entire sequence of events that preceded the deletion. (Unless of course you need some kind of versioning ).
Or some piece of data is deleted remotely while you were offline and you have a pending local request to edit that data. The update journal can be essential here in sorting out those potential conflicting operations.
- The syncing process is a multi-step operation
1) Fetch updates from remote database (always first ).
2) Examine updates against the local journal in order to deal with conflicts and redundancies -
drop any remote or local updates that no longer make sense for example.
3) Apply remote updates
4) Push local updates.
Finally, if necessary you might consider a hybrid option as well:
One that requires a network connection for certain critical updates - i.e. to check against the remote database in order to avoid creating document with the same email address or name locally.
Core Data : https://www.amazon.com/Core-Data-Storage-Management-iCloud/dp/1937785084
https://xamarinhelp.com/mobile-database-bi-directional-synchronization-rest-api/ (some key practical insights here )