Search code examples
swiftcore-datacloudkit

Couldn't initialize CloudKit schema because no stores in the coordinator are configured to use CloudKit


I'm getting this error when trying to initialize the Cloudkit schema:

Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=Couldn't initialize CloudKit schema because no stores in the coordinator are configured to use CloudKit: ()}

This is my code:

private lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentCloudKitContainer(name: self.objectModelName)
        
        // Create a store description for a CloudKit-backed local store
        let cloudStoreLocation = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("iCloud.sqlite")
        let cloudStoreDescription = NSPersistentStoreDescription(url: cloudStoreLocation)
        cloudStoreDescription.configuration = "iCloud"

        // Set the container options on the cloud store
        cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.myname.app")
        
        // Update the container's list of store descriptions
        container.persistentStoreDescriptions = [
            cloudStoreDescription
        ]
        
        do {
            try container.initializeCloudKitSchema()
        } catch {
            print(error)
        }
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

This is my: project configuration

This is my: data model schema

Can you anyone help me spot what I'm doing wrong?

Thanks!


Solution

  • For god's sake, I just had to sign-in with my AppleID into the iPhone's Settings, and also enable iCloud Drive in Settings > My Name > iCloud in the same device.

    This wasn't explicitly mentioned in the tutorial on Apple's developer docs.