I am building an app leverage the CoreDataStack method described in the book, however when I show my user the appropriate entry and then allow them to go to an “Edit” screen to update the entry… it crashes on the managedContext.save() and the error catch doesn’t seem to provide any details. Here’s the code I use to do the save…
` @IBAction func saveRecipient(_ sender: Any) {
if let firstNameText = firstName.text {
selectedRecipient = Recipient(context: managedContext)
selectedRecipient?.firstName = firstNameText
selectedRecipient?.lastName = lastName.text!
selectedRecipient?.addressLine1 = addressLine1.text!.capitalized(with: NSLocale.current)
selectedRecipient?.addressLine2 = addressLine2.text!.capitalized(with: NSLocale.current)
selectedRecipient?.state = state.text!.uppercased()
selectedRecipient?.city = city.text!.capitalized(with: NSLocale.current)
selectedRecipient?.zip = zipCode.text!
selectedRecipient?.country = country.text!.capitalized(with: NSLocale.current)
}
do {
try managedContext.save()
} catch let error as NSError {
print("Save error: \(error), \(error.userInfo)")
}
navigationController?.popViewController(animated: true)
}
`
What is the best practice for doing a entry update?