Posts

Post marked as solved
13 Replies
I spoke to an Apple Engineer about this and they suggested the best way was to make your container return either an NSPersistentContainer or NSPersistentCloudKitContainer depending on whether you users have selected iCloud syncing internally in your app - you can use a UserDefaults bool to store this.This works because NSPersistentCloudKitContainer is a subclass of NSPersistentContainer.You will also need to set the NSPersistentHistoryTrackingKey to true on the vanillla container so changes are recorded should they switch iCloud back on. There doesn't appear to be any need to set any options manually on NSPersistentCloudKitContainer as they're enabled by default.I have a PersistenceService class which manages my MOC and in it, this is how I set up the container: static var iCloudSyncData = UserDefaults.standard.bool(forKey: "iCloudSyncData") static var persistentContainer: NSPersistentContainer = { let container: NSPersistentContainer? if iCloudSyncData { container = NSPersistentCloudKitContainer(name: "MYAPP") } else { container = NSPersistentContainer(name: "MYAPP") let description = container!.persistentStoreDescriptions.first // This allows a 'non-iCloud' sycning container to keep track of changes if a user changes their mind // and turns it on. description?.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) } container!.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { // Handle the errors fatalError("Unresolved error \(error), \(error.userInfo)") } }) // As NSPersistentCloudKitContainer is a subclass of NSPersistentContainer, you can return either. return container! }()This all works beautifully on my devices, and syncing (and toggling on/off) all works... but despite 'Deploying the Schema to Production' not one of my users in the 'real world' is able to sync their data with CloudKit. It seems like there is something wrong with the way their devices initially subscribe and I can't find an easy way to troubleshoot this as the API is somewhat opaque. I'm in discussions with an Apple Engineer about this so will hopefully have a solution soon.
Post not yet marked as solved
6 Replies
I suspect it's just because it's still very much in the beta phase.I have an app with about 3600 core data managed objects and they appear to be uploading OK (although it's hard to really know without any sort of % progress feedback). I did get a lot of "Limit Exceeded" errors (syncing more than 400 CKRecords at a time isn't allowed) but it seemed to automatically divide the requests and reschedule them.However, on my second device, only a fraction of the data has come down and there is no way to tell what percentage has completed so it's anyone's guess as to whether it is 'done' or not...So far, I'm very impressed that it works at all given that it required effectively no code (especially all that complicated error handling), but I think they still have a way to go before I'm confident enough to roll it out to my users.