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

Ant*_*che 4 core-data swift cloudkit

我在尝试初始化 Cloudkit架构时收到此错误:

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: ()}
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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
    }()
Run Code Online (Sandbox Code Playgroud)

这是我的:项目配置

这是我的:数据模型架构

你能帮我找出我做错了什么吗?

谢谢!

小智 13

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

我打了电话

container.initializeCloudKitSchema()
Run Code Online (Sandbox Code Playgroud)

打电话之前

container.loadPersistentStores
Run Code Online (Sandbox Code Playgroud)

您需要在调用container.loadPersistentStores后调用container.initializeCloudKitSchema


Ant*_*che 6

天哪,我只需要使用 AppleID 登录 iPhone 的“设置”,并在Settings > My Name > iCloud同一设备中启用 iCloud Drive。

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