首页 > 解决方案 > WidgetKit中带有CloudKit的CoreData不起作用

问题描述

在我的应用程序中,我NSPersistentCloudKitContainer通过以下方式进行了设置:

    let container: NSPersistentCloudKitContainer

    init() {
        self.container = NSPersistentCloudKitContainer(name: "Model")

        self.container.persistentStoreDescriptions[0].setOption(
            true as NSNumber,
            forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey
        }

        self.container.loadPersistentStores { (_, error) in
            guard error == nil else {
                print("error loading store: \(error.debugDescription)")
                return
            }
        }
        
        self.container.viewContext.automaticallyMergesChangesFromParent = true
    }

当我在我的 iOS 目标中对容器的上下文调用 fetch 请求时,它按预期工作。但是,当我在 Widget 目标中执行相同的 fetch 请求时,它会返回一个空数组结果。

在我的 iOS 目标中,我有后台模式/远程通知和 iCloud/CloudKit 功能。在我的 Widget 目标中,我具有 iCloud / CloudKit 功能。

不知道为什么它不起作用,所以任何帮助将不胜感激,谢谢!

标签: ioscore-datacloudkitwidgetkit

解决方案


推荐阅读