Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get old and new value of property of NSPropertyDescription

I have a CoreData stack set up, able to retrieve the edit history for specific objects and so forth. I can even identify which property changed. However I'm struggling with getting the old and new value of the property. See my code below. Is this even possible? If so, how?

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:
        // ...
    }
    // ...

}
like image 381
henrik-dmg Avatar asked Jun 16 '19 17:06

henrik-dmg


1 Answers

Problem I was facing:

  • I couldn't identify the record to update in the destination store.

Workaround:

  • Use an identifier field in the Entity
  • That way you can query based on the identifier and update destination store with the new values
  • identifier will not / should not change after insertion
like image 135
user1046037 Avatar answered Oct 19 '22 23:10

user1046037