WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

How to toggle iCloud Sync ?

Forums > SwiftUI

I would like to implement a toggle button so people can choose if they want to sync with icloud anytime.

I found https://fluffy.es/toggle-icloud-sync-nspersistentcloudkitcontainer/ , this seems working well, except two problems happen when user switch "not sync" to "sync" in one of device:

  1. In debug windows, it shows:

NSLocalizedFailureReason=CloudKit setup failed because there is another instance of this persistent store actively syncing with CloudKit in this process., NSUnderlyingException=Illegal attempt to register a second handler for activity identifier

  1. the cloud data overwrite the local new data, even I am using NSMergeByPropertyObjectTrumpMergePolicy

if I created only once PersistentCloudContainer in init, and change cloudKitContainerOptions + loadPersistentStores everytime user toggle, there will be error during loadPersistentStores: NSUnderlyingException : Can't add the same store twice

Any tips? Thank you very much!


Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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.