Search code examples
iosuitableviewswiftcore-datansmanagedobject

Correct way to update Core Data


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.


Solution

  • 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?