Update an Existing Xcode Project Core Data with CloudKit & Manage Multiple Stores

Hi,

I have.a project that uses CoreData and I want to add some of the data to iCloud to sync with a Watch app and I ticked the option for the Default configuration to use iCloud and It worked but now I don't want to include images in the cloud because it became too large so I Seperated the images into a different data thing and as per instructions here https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/setting_up_core_data_with_cloudkit I added a configuration "Local" and "Cloud" and change the code as below -

original
Code Block
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DataModel")
container.loadPersistentStores(completionHandler: {
storeDescription, error in
if let error = error {
print("Could load data store: \(error)")
}
})
print("Loaded data store: DataModel")
return container
}()


I created two configurations as per the video and ticked use with iCloud on the one I would like to sync with iCloud.

new code
Code Block
lazy var persistentContainer: NSPersistentContainer = {
var container = NSPersistentContainer(name: "DataModel")
if #available(iOS 13.0, *) {
container = NSPersistentCloudKitContainer(name: "DataModel")
let local = NSPersistentStoreDescription(url: URL(fileURLWithPath: "/files/local.sqlite"))
local.configuration = "Local"
let cloud = NSPersistentStoreDescription(url: URL(fileURLWithPath: "/files/cloud.sqlite"))
cloud.configuration = "Cloud"
cloud.cloudKitContainerOptions =
NSPersistentCloudKitContainerOptions(
containerIdentifier: "iCloud.com.AppName")
container.persistentStoreDescriptions = [ local, cloud ]
container.loadPersistentStores { storeDescription, error in
guard error == nil else {
fatalError("Could not load persistent stores. \(error!)")
}
}
return container
} else {
container.loadPersistentStores(completionHandler: {
storeDescription, error in
if let error = error {
print("Could load data store: \(error)")
}
})
print("Loaded data store: DataModel")
return container
}
}()


Most of this code I got from the video https://developer.apple.com/videos/play/wwdc2019/202/

but I keep getting an error about loading or saving the persistent store or something

fatal error: Could not load persistent stores. Error Domain=NSCocoaErrorDomain Code=512 "The file couldn’t be saved." UserInfo={reason=Failed to create file; code = 2}:

Is anyone able to point out what I am not doing right?
Thanks.
Hi, I think I found the answer

Code Block
let storeDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
let url = storeDirectory.appendingPathComponent("local.sqlite")
let local = NSPersistentStoreDescription(url: url)
...
let cloudUrl = storeDirectory.appendingPathComponent("cloud.sqlite")
let cloud = NSPersistentStoreDescription(url: cloudUrl)


Something to do with the path as I thought.
Changing those lines seams to have made it work.
Update an Existing Xcode Project Core Data with CloudKit & Manage Multiple Stores
 
 
Q