Did you get this to work? I have a similar need, that is, to offer iCloud storage and sync as a premium feature. I have CloudKit sync working but how do I switch it off? I want to disable CloudKit sync and only enable it when a customer pays for the premium version. I've tried setting the container's cloudKitContainerOptions property to nil but CloudKit sync still happens.
Here's my Core Data stack:
private lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: modelName)
let localDescription = NSPersistentStoreDescription(url: configurationsURL.appendingPathComponent("local.sqlite", isDirectory: false))
localDescription.configuration = "Local"
let cloudDescription = NSPersistentStoreDescription(url: configurationsURL.appendingPathComponent("cloud.sqlite", isDirectory: false))
cloudDescription.configuration = "Cloud"
cloudDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: cloudKitContainerIdentifier)
cloudDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
cloudDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
container.persistentStoreDescriptions = [localDescription, cloudDescription]
container.loadPersistentStores { (storeDescription, error) in
if let error = error as NSError? {
fatalError("###\(#function): Failed to load persistent stores:\(error)")
}
}
container.viewContext.name = "main"
container.viewContext.transactionAuthor = appTransactionAuthorName
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
container.viewContext.automaticallyMergesChangesFromParent = true
do {
try container.viewContext.setQueryGenerationFrom(.current)
} catch {
fatalError("###\(#function): Failed to pin viewContext to the current generation:\(error)")
}
NotificationCenter.default.addObserver(self, selector: #selector(type(of: self).storeRemoteChange(_:)), name: .NSPersistentStoreRemoteChange, object: nil)
// do {
// //try container.initializeCloudKitSchema(options: [.dryRun, .printSchema])
// try container.initializeCloudKitSchema()
// } catch {
// let nserror = error as NSError
// print("Could not initialize CloudKit schema: \(error.localizedDescription)")
// print(nserror.code)
// print(nserror.domain)
// }
return container
}()
Ands here's a function where I'm trying to toggle the CloudKit sync:
func enableiCloud(_ isEnabled: Bool) {
let cloudDescription = persistentContainer.persistentStoreDescriptions.first() {
$0.configuration == "Cloud"
}
guard cloudDescription != nil else { return }
if isEnabled {
cloudDescription!.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: cloudKitContainerIdentifier)
cloudDescription!.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
} else {
cloudDescription!.cloudKitContainerOptions = nil
cloudDescription!.setOption(false as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
}
}