Implementing Local database sync with remote database

Hi guys, first post in the forum for me. I'm fairly new to swift (nearly completed Apple's "App Development With Swift") and have done a couple of simple apps to help some family projects. I have only used Codable protocol and stored data in .plist files.
So the task described in the title is proving to be a challenge for me.
Can someone point me towards some books, or online articles regarding how to implement local database sync with remote database? my goal is to have the app work both off and online and check for updates whenever there is a connection to the internet, which would then download the new data (possibly thousands of records) and update the local database with it.

Thanks in advance!
Renato

That's probably a question they can answer better at the Apple developer forums than we can here.

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.

Book ref:
Core Data : https://www.amazon.com/Core-Data-Storage-Management-iCloud/dp/1937785084

Blogs:
https://www.objc.io/issues/10-syncing-data/sync-case-study/
http://inessential.com/vespersyncdiary
https://xamarinhelp.com/mobile-database-bi-directional-synchronization-rest-api/ (some key practical insights here )

Good luck :smile:

2 Likes

Hi checkout this book https://www.objc.io/books/core-data/ by Florian Kugler it contains a chapter on how to sync core data objects with web services.

1 Like

Thank you for the reply. Great insight and really helpful. I'm going to do some research on local stores, but I generally tend to opt for apple APIs simply due to them "owning" the OS and hardware which will run everything.
Do you think there is a benefit from choosing Core Data vs something like Realm ?

Terms of Service

Privacy Policy

Cookie Policy