CoreData on Mac OS is not saving. Can't figure out why?

Hello:


I bult an app for iOS successfully using CoreData. Now I am working on an app on Mac OSx with the latest version of Swift and Xcode.

However, I am unable to save any data. I am relatively new to Xcode so my problem, I'm sure, is due to a lack of expertise with programming in Swift. (I have done a lot of work with Oracle).


I am currently trying to save information entered into text fields to CoreData, without success.

Below is my code. I get no runtime errors. I hope someone can point me to the changes I need to make.


  1. import Cocoa
  2. import CoreData
  3. class Register: NSViewController {
  4. var items: [NSManagedObject]
  5. requiredinit?(coder aDecoder: NSCoder) {
  6. self.items = []
  7. super.init(coder: aDecoder)
  8. }
  9. overridefunc viewDidLoad() {
  10. super.viewDidLoad()
  11. if let firstNameTextField = firstNameTextField {
  12. firstNameTextField.delegate = (self as NSTextFieldDelegate)
  13. }
  14. if let middleNameTextField = middleNameTextField {
  15. middleNameTextField.delegate = (self as NSTextFieldDelegate)
  16. }
  17. if let lastNameTextField = lastNameTextField {
  18. lastNameTextField.delegate = (self as NSTextFieldDelegate)
  19. }
  20. if let birthdateTextField = birthdateTextField {
  21. birthdateTextField.delegate = (self as NSTextFieldDelegate)
  22. }
  23. if let schoolTextField = schoolTextField {
  24. schoolTextField.delegate = (self as NSTextFieldDelegate)
  25. }
  26. if let schoolAddressTextField = schoolAddressTextField {
  27. schoolAddressTextField.delegate = (self as NSTextFieldDelegate)
  28. }
  29. if let schoolCityTextField = schoolCityTextField {
  30. schoolCityTextField.delegate = (self as NSTextFieldDelegate)
  31. }
  32. if let schoolStateTextField = schoolStateTextField {
  33. schoolStateTextField.delegate = (self as NSTextFieldDelegate)
  34. }
  35. if let emailTextField = emailTextField {
  36. emailTextField.delegate = (self as NSTextFieldDelegate)
  37. }
  38. }
  39. @IBOutletweakvar firstNameTextField: NSTextField!
  40. @IBOutletweakvar middleNameTextField: NSTextField!
  41. @IBOutletweakvar lastNameTextField: NSTextField!
  42. @IBOutletweakvar birthdateTextField: NSTextField!
  43. @IBOutletweakvar schoolTextField: NSTextField!
  44. @IBOutletweakvar schoolAddressTextField: NSTextField!
  45. @IBOutletweakvar schoolCityTextField: NSTextField!
  46. @IBOutletweakvar schoolStateTextField: NSTextField!
  47. @IBOutletweakvar emailTextField: NSTextField!
  48. @IBActionfunc saveRegistrant(_ sender: Any) {
  49. print("SAVE ACTION STARTED")
  50. let appDelegate = NSApplication.shared.delegate as! AppDelegate
  51. let managedContext = appDelegate.persistentContainer.viewContext
  52. let entity = NSEntityDescription.entity(forEntityName: "Registration", in: managedContext)!
  53. let registerStudentsObject = NSEntityDescription.insertNewObject(forEntityName: "Registration", into: managedContext)
  54. print("MOC READ")
  55. let component = NSManagedObject(entity: entity, insertInto: managedContext)
  56. registerStudentsObject.setValue(firstNameTextField, forKeyPath: "firstName")
  57. registerStudentsObject.setValue(middleNameTextField, forKey: "middleName")
  58. registerStudentsObject.setValue(lastNameTextField, forKey: "lastName")
  59. registerStudentsObject.setValue(birthdateTextField, forKey: "dob")
  60. registerStudentsObject.setValue(schoolTextField, forKey: "school")
  61. registerStudentsObject.setValue(schoolAddressTextField, forKey: "schoolAddress")
  62. registerStudentsObject.setValue(schoolCityTextField, forKey: "schoolCity")
  63. registerStudentsObject.setValue(schoolStateTextField, forKey: "schoolState")
  64. registerStudentsObject.setValue(emailTextField, forKey: "email")
  65. print("FIELDS READ")
  66. do
  67. {
  68. try managedContext.save()
  69. items.append (component)
  70. print("saved")
  71. }
  72. catch
  73. {
  74. }
  75. }
  76. }
  77. extension Register: NSTextFieldDelegate {
  78. func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
  79. if let textField = control as? NSTextField {
  80. print(textField.stringValue)
  81. }
  82. return true
  83. }
  84. }

Accepted Reply

hi,


first, i endorse alienspaces's comment that you should really implement a catch on managedContext.save() to see why there is no saving.


but then second, and i'm not sure if it's the immediate problem, i am suspicious of what you are trying to save in Core Data -- for example, in line 58:


registerStudentsObject.setValue(firstNameTextField, forKeyPath: "firstName")


the variable firstNameTextField is of type NSTextField; i'd guess you really want to save the text in the firstNameTextField with


registerStudentsObject.setValue(firstNameTextField.stringValue, forKeyPath: "firstName")



hope that helps,

DMG

Replies

You could start by actually catching the error in your catch clause and printing it out. Add breakpoints and step-debug through it to see what's actually getting called and what's not. Often, it's assumptions we hold that block progress.

hi,


first, i endorse alienspaces's comment that you should really implement a catch on managedContext.save() to see why there is no saving.


but then second, and i'm not sure if it's the immediate problem, i am suspicious of what you are trying to save in Core Data -- for example, in line 58:


registerStudentsObject.setValue(firstNameTextField, forKeyPath: "firstName")


the variable firstNameTextField is of type NSTextField; i'd guess you really want to save the text in the firstNameTextField with


registerStudentsObject.setValue(firstNameTextField.stringValue, forKeyPath: "firstName")



hope that helps,

DMG

Hello:

You were EXACTLY RIGHT!

My data is saving to CoreData beautifully.

Thanks very much.