UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Can iCloud Documents be used to sync simple data betwen devices?

Forums > Swift

I'm working on a simple app (think to-do list). There are two pieces of data that I would like to sync between devices, users' to-do lists (in json format) and user settings (theme color, sound volume, etc.). Also, I would like to have a watchOS extension to view lists and check off items on a list.

So I have a few questions:

  1. Would iCloud documents be enough for something small like this?
  2. Can you sync iCloud documents between devices (including a watch)?
  3. Do iCloud documents work off-line? Can you use iCloud documents just to update a local copy? Or how does that work?
  4. Do iCloud documents deal with syncing conflicts or do you have to implement your own logic for that?
  5. I looked into CloudKit, but looks like overkill. Or is it worth the effort?

3      

For preferences you can use NSUbiquitousKeyValueStore. It is basically like UserDefaults but synced automatically between devices.

By iCloud Documents I assume you mean the iCloud Drive? This works offline just fine, because you can just save the file there and system will sync it when available. However I am not aware of any "smart" system for resolving conflicts + user can access these files outside your app.

3      

Thanks, @nemecek-filip. I'm not sure whether iCloud Drive and iCloud Documents are the same thing. I can't find much on the subject. Users being able to modify the files in another program would be a big no-no go for me.

I guess I'll do a test, and see whether it can be opened with a different program.

3      

For anyone looking into this in the future, deciding whether users can see the folder in the FIles app can be handled with the NSUbiquitousContainerIsDocumentScopePublic key in info.plist. This is one of the 3 keys required when configuring the iCloud Documents container. There are also some weird rules around changes to this value not being reflected unless the build number is incremented or something like that.

NSUbiquitousKeyValueStore is pretty easy but has its downsides, namely that there's no way to handle conflicts and that if there are too many updates queued (this happens a lot durring development) or if the user force quits the app, changes will not sync. There is a pod called https://github.com/ArtSabintsev/Zephyr that uses both UserDefaults and KVS to always check which version is newest, but I haven't tested this in a real app yet.

Andrew Bancroft has a nice course on pluralsight that goes into more detail about the different options for data persistance in iOS (including these two): https://www.andrewcbancroft.com/2019/04/30/ios-data-persistence-the-big-picture/

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.