CoreData+CloudKit custom Zone

Hi,

I'm using CoreData+CloudKit in a blank universal project for macOS 11 and iOS14.
The records are in a private database inside a custom zone, I was able to set the correct database, but I'm yet to be able to set the zone.

On the PersistentController created by default by Xcode 12 I have the following code
Code Block
let container: NSPersistentCloudKitContainer
    init(inMemory: Bool = false) {
        container = NSPersistentCloudKitContainer(name: "finances_manager_pro")
        let customZone = CKRecordZone(zoneName: "CloudCore")
        guard let description = container.persistentStoreDescriptions.first else {
            fatalError("Error")
        }
        description.cloudKitContainerOptions?.databaseScope = .private
        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
    }


And inside a View
Code Block
@FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \ExpenseCoreData.timestamp, ascending: true)],
        animation: .default)
    private var items: FetchedResults<ExpenseCoreData>


I've searched around the web and in apple docs but I haven't found an updated way to do this.

Did you ever manage to work this one out?

Are you using manually syncing your CloudKit with CoreData or are you using NSPersistentCloudKitContainer?

If you using NSPersistentCloudKitContainer does the syncing for you, don't have to set any custom zone, it would set a custom zone automatically.

If you are doing it manually, then you would need to set a custom zone when you are saving a record and when you are retrieving it not while loading the container's persistent stores (which is what you are doing).

CoreData+CloudKit custom Zone
 
 
Q