如何获取 NSPropertyDescription 属性的旧值和新值

hen*_*dmg 6 core-data ios swift

我设置了一个 CoreData 堆栈,能够检索特定对象的编辑历史记录等。我什至可以识别哪些属性发生了变化。然而,我正在努力获取该房产的新旧价值。请参阅下面的我的代码。这可能吗?如果是这样,怎么办?

func apply(_ changeItem: NSPersistentHistoryChange) {
    switch changeItem.changeType {
    case .update:
        guard let updatedProperties = changeItem.updatedProperties else { break }

        updatedProperties.forEach { (property) in
            // Get old and new value here
        }
    case .insert:
        // ...
    }
    // ...

}
Run Code Online (Sandbox Code Playgroud)

use*_*037 1

我面临的问题:

  • 我无法识别目标存储中要更新的记录。

解决方法:

  • 在实体中使用标识符字段
  • 这样您就可以根据标识符进行查询并使用新值更新目标存储
  • 插入后标识符不会/不应该改变