核心数据+ CloudKit是否无法在其他设备上自动刷新? [英] Core Data + CloudKit not refreshing on other device automatically?
问题描述
我的SwiftUI应用程序与Core Data + CloudKit一起使用.我可以从Mac或iPhone将新条目保存到数据库中.该应用程序对所有设备使用完全相同的项目/代码库.
I have my SwiftUI app working with Core Data + CloudKit. I am able to save new entries to the database from my Mac or iPhone. The app uses the exact same project/codebase for all devices.
唯一的问题是我需要关闭该应用程序,然后重新打开它才能查看从另一台设备进行的任何新条目.我可以立即看到同一设备上的新条目,但是在我关闭并在其上重新打开应用程序后,它们才会出现在其他设备上.
The only problem is that I need to close out of the app and re-open it to see any new entries made from the other device. I can immediately see the new entries made from the same device, but they do not appear on the other devices until I close and re-open the app on it.
我在这里错过了什么吗?我是否需要在Entity类中添加ObservableObject或Identifiable之类的内容?我该如何解决这个问题?
Am I missing something here? Do I need to add something like ObservableObject or Identifiable to my Entity class? How do I correct this issue?
这是我获取记录的方式:
Here is how I obtain the records:
@FetchRequest(
entity: Doc.entity(),
sortDescriptors: [NSSortDescriptor(key: "title", ascending: true)]
) var docs: FetchedResults<Doc>
推荐答案
想通了!
我必须添加:
context.automaticallyMergesChangesFromParent = true
转到 SceneDelegate
中的 scene()
函数:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
context.automaticallyMergesChangesFromParent = true
现在可以正常工作了.
这篇关于核心数据+ CloudKit是否无法在其他设备上自动刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!