【问题标题】:Does the @FetchRequest property wrapper automatically notify when changes happen?@FetchRequest 属性包装器是否会在发生更改时自动通知?
【发布时间】:2020-11-23 23:20:03
【问题描述】:

如果我使用的是 NSPersistentCloudKitContainer,我会在 sqlite 文件中看到更改会自动同步。我还可以在 CloudKit 仪表板中进行更改,重新启动应用程序并查看更改。如果我使用@FetchRequest 加载一些项目,项目数组是否应该自动更新?

// Fetch some items
@FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
        animation: .default)
    private var items: FetchedResults<Item>

// Show them in a List

List(items) { item in
  VStack {
    Text("Item at \(item.timestamp!, formatter: itemFormatter)")
  }
}

【问题讨论】:

    标签: core-data swiftui


    【解决方案1】:

    是的,如果您在勾选使用 CoreData 和 CloudKit 时设置了不在 Apple 提供的默认代码中的 automaticallyMergesChangesFromParent = true

    let container = NSPersistentCloudKitContainer(name: "MyApp")
    container.viewContext.automaticallyMergesChangesFromParent = true
    

    【讨论】:

      猜你喜欢
      • 2021-08-25
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多