无法初始化 CloudKit 架构,因为协调器中没有任何存储被配置为使用 CloudKit

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

我在尝试初始化 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: ()}

这是我的代码:

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
    }()

这是我的:project configuration

这是我的:data model schema

谁能帮我找出我做错了什么?

谢谢!

看在上帝的份上,我只需要使用我的 AppleID 登录 iPhone 的设置,并在同一设备的 Settings > My Name > iCloud 中启用 iCloud Drive。

Apple 开发者文档的教程中没有明确提到这一点。

谁以后可能会遇到这个。我有同样的错误,问题与上面的示例代码相同。

我打电话

container.initializeCloudKitSchema()

调用前

container.loadPersistentStores

您需要在调用 container.loadPersistentStores

之后调用 container.initializeCloudKitSchema