Just got another model working by mostly try and error. Used with CloudKit is checked, that is inside Cloud configuration's inspector
let defaultDirectoryURL = NSPersistentContainer.defaultDirectoryURL()
let cloudStoreURL = defaultDirectoryURL.appendingPathComponent("Cloud.sqlite")
let cloudStoreDescription = NSPersistentStoreDescription(url: cloudStoreURL)
cloudStoreDescription.configuration = "Cloud"
cloudStoreDescription.cloudKitContainerOptions =
NSPersistentCloudKitContainerOptions(
containerIdentifier: "iCloud.com.example.app")
cloudStoreDescription.cloudKitContainerOptions?.databaseScope = .public
cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
cloudStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
let localStoreURL = defaultDirectoryURL.appendingPathComponent("Local.sqlite")
let localStoreDescription = NSPersistentStoreDescription(url: localStoreURL)
localStoreDescription.configuration = "Local"
persistentContainer.persistentStoreDescriptions = [ cloudStoreDescription, localStoreDescription ]
persistentContainer.loadPersistentStores(completionHandler: { (_, error) in
guard let error = error as NSError? else { return }
fatalError("###\(#function): Failed to load persistent stores:\(error)")
})
persistentContainer.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
persistentContainer.viewContext.transactionAuthor = appTransactionAuthorName
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
do {
try persistentContainer.viewContext.setQueryGenerationFrom(.current)
} catch {
fatalError("###\(#function): Failed to pin viewContext to the current generation:\(error)")
}
}