Core Data. Saving new, loading old data. How to update the Core Data?

时间:2016-04-04 18:58:49

标签: ios swift core-data

I have couple of times, experienced problem with updating data to existing "row" in an Entity in Core Data. After saving to core data, I will do a fetch, that retrieves the old data. After a restart, the latest saved data will show. Can anyone help me please?

Thanks

Code that what update before restart:

func saveComments(){
    let comment = textBox.text

    let fetchRequest = NSFetchRequest(entityName: "Article")
    fetchRequest.predicate = NSPredicate(format: "title = %@", (article?.title)!)

    do{
        let result = try moc.executeFetchRequest(fetchRequest) as? [NSManagedObject]
        let articleToUpdate = result![0]
        articleToUpdate.setValue(comment, forKey: "comments")
    }catch{
        fatalError()
    }

    do{
        try moc.save()
    }catch{
        fatalError()
    }

}

0 个答案:

没有答案