I am trying to get the data I update in an entity within CoreData to save; however, I change the values and yet it doesn't save. Here's the basics of how I am doing this:
var container: NSPersistentContainer!
var theSettings: Settings!
var collectionSize: Int
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = container.viewContext
theSettings = Settings.init(context: context)
collectionSize = Int(theSettings.defaultSize) // This works sets the value correctly
// Later when the user changes a setting I do this
theSettings.defaultSize = 6
do {
try appDelegate.persistentContainer.viewContext.save()
} catch let error as NSError {
print("Error: \(error), \(error.userInfo)")
}
I've checked and the persistentContainer is correctly setup in the appDelegate. However, the value goes back to the default once I leave the view. It never saves. I've checked all over and I can't see anything that I'm doing wrong. I've done a lot of iOS programming, but this is the first time I'm using CoreData.
Thanks for any help you can offer.