【问题标题】:Core Data + CloudKit not refreshing on other device automatically?Core Data + CloudKit 不会在其他设备上自动刷新?
【发布时间】:2019-11-14 15:59:51
【问题描述】:

我的 SwiftUI 应用程序使用 Core Data + CloudKit。我可以从我的 Mac 或 iPhone 将新条目保存到数据库中。该应用对所有设备使用完全相同的项目/代码库。

唯一的问题是我需要关闭应用程序并重新打开它才能查看从其他设备生成的任何新条目。我可以立即看到从同一设备创建的新条目,但它们不会出现在其他设备上,直到我关闭并重新打开该设备上的应用程序。

我在这里遗漏了什么吗?我是否需要在我的 Entity 类中添加诸如 ObservableObject 或 Identifiable 之类的东西?我该如何解决这个问题?

编辑:

这是我获取记录的方式:

@FetchRequest(
    entity: Doc.entity(),
    sortDescriptors: [NSSortDescriptor(key: "title", ascending: true)]
) var docs: FetchedResults<Doc>

【问题讨论】:

    标签: ios swift core-data swiftui cloudkit


    【解决方案1】:

    想通了!

    我必须补充:

    context.automaticallyMergesChangesFromParent = true
    

    到SceneDelegate中的scene()函数:

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    context.automaticallyMergesChangesFromParent = true
    

    现在它的工作完美无缺。

    信用:https://www.andrewcbancroft.com/blog/ios-development/data-persistence/getting-started-with-nspersistentcloudkitcontainer/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 2017-04-20
      • 1970-01-01
      • 2022-07-03
      • 2011-01-10
      • 2022-11-16
      • 2021-07-19
      • 1970-01-01
      相关资源
      最近更新 更多