Post marked as solved
Click to stop watching this thread.
You have stopped watching this post. Click to start watching again.
contentPostList.repliessolved.tooltip
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.