Reputation: 1974
I'm trying to update my Core Data stack when a button gets pressed. it all looks fine when the app is running in one session, but when I restart the app the update I made no longer exist.
My code looks like this:
if let indexPath = self.tv.indexPathForRowAtPoint(point){
let data = messageList[indexPath.row] as Messages
if data.read == true{
println("Message already read")
}else{
data.read = true
}
}
Why does this happen? The change always get reverted when my app is being restarted.
Upvotes: 1
Views: 130
Reputation: 5656
You nedd to save NSManagedObjectContext before close your application.
For example: AppDelegate.swift
func applicationWillTerminate(application: UIApplication) {
self.saveContext()
}
This works only if you kill the app.
But if you want to save context after any change in NSManagedObject, you need to call mangedObjectContext.save(&error)
Upvotes: 1
Reputation: 8164
You have to ensure, that you call save
on the NSManagedObjectContext
instance. There should be at least on call already defined in AppDelegate
, but you might want to call it more often, e.g. when your app enters the background or on similar occasions.
For further reference, take a look at iOS Core Data when to save context?
Upvotes: 1