How to sync SQLite database between two devices through the same app?

Hi,


I'm doing my first project using Xcode8 and Swift3, I wrote my database using SQLite which means it's local storage without server. Now I wann sync database between two devices but can I do it without server? I suppose it's quite hard.

If I can't do it without server, I suppose I can do it with iCloud as they are using the same AppleID, but if I use iCloud, what should I do? should I change my database into coreData? if so, how?


Thank you very much!!!

Accepted Reply

What if when two device are under the same wifi, and then device can be allocated a certain IP adress?

You don’t need to hard-wire IP addresses; the devices can discover each other via Bonjour. The WiTap sample code is a good example of this.

However, getting data moving between the devices isn’t really the hard part of the problem. As I mentioned before, the hard parts are security and syncing.

You might want to take a look at my (slightly long in the tooth) technote on this topic, Technote 2152 Document Transfer Strategies.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Syncing is quite difficult in the general case. For most people the best option is to store the ‘truth’ in iCloud (via CloudKit) and then have each device’s local copy be a cache of that truth. However, even that can get really tricky if you allow offline modifications.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

But I want to make it without server, if I store the "truth" in iCloud as you said, isn't that kind of server-clients?

I was thinking if I can use TCP/IP or HTTP to write a protocol in swift that record the modifications on the database, then communicate to another device's application then make synchronisation through this?

You can use/rely on traditional db sync, but then you have to deal with edits, cached adds, updates etc., and unless you are a db maven, there lies grief when it involves mobile devices, I think.

But I want to make it without server …

It’s certainly possible to do peer-to-peer synching but that just makes things harder. Syncing is a really hard problem in general. Peer-to-peer syncing adds additional complexities like:

  • User interface — iOS apps typically only run when they’re in the foreground, which means that your user interface ends up being kinda clunky because you need to bring the apps to the front on both devices in order to sync.

  • Security — This includes both on-the-wire privacy, authentication and authorisation.

Looking back at the syncing problem itself, iOS has no high-level APIs for that. There’s various building blocks (file system and database APIs, networking APIs, security APIs) but you’ll have to solve the core syncing problem yourself (read up on vector clocks, CRDTs, and so on) or work with some third-party library that does this (for example, Ensembles).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

What if when two device are under the same wifi, and then device can be allocated a certain IP adress? then I can use one device as server then another one as client. so that they can communicate with each other using HTTP when wifi is connected?

What if when two device are under the same wifi, and then device can be allocated a certain IP adress?

You don’t need to hard-wire IP addresses; the devices can discover each other via Bonjour. The WiTap sample code is a good example of this.

However, getting data moving between the devices isn’t really the hard part of the problem. As I mentioned before, the hard parts are security and syncing.

You might want to take a look at my (slightly long in the tooth) technote on this topic, Technote 2152 Document Transfer Strategies.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I've looked it up. I stiil can't really understand the using of Bonjour, I can download that project (WiTap), but how can another device connected to the simulator?


Sorry for replying late, I was in Cornwall the early days.

I've looked it up. I stiil can't really understand the using of Bonjour, I can download that project (WiTap), but how can another device connected to the simulator?

WiTap works just fine in the simulator. If you run WiTap once on the simulator and once on a real device, they should be able to see each other and connect (assuming the Mac and the iOS device are on the same Wi-Fi).

… I was in Cornwall the early days.

Cool. My family takes our summer holidays in Cornwall each year.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I just tried it but WiTap is written in OC, I don't know how to rewrite them in Swift XD, what a silly goose😐Do you have any reference to look at about rewrite code from OC to Swift?


Cornwall is so beautiful!!!! and I went to st.Ives, it's really small and just pefect for chill. But I suppose it's quite difficult to get there without a car.


and

You are really helpful!!!!! Thank you very much Eskimo!

Best regards.

Grace

Do you have any reference to look at about rewrite code from OC to Swift?

Not off the top of my head, sorry. But WiTap uses pretty vanilla Objective-C, so adopting the core concepts to Swift shouldn’t be too tricky.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Supper Thank you very much Eskimo!!!


Enjoy your day!