0
  • I have update a NSModelObject and saved using the following code, the changes are saved and updated in app.

    do {
        try managedObjectContext.save()
    } catch {
        print("Could not save")
    }
    
  • But when I kill the app and re-launch it the updated changes are lost.

Note:

  • I am using Restkit for mapping those data in db
  • I also check "saveContext()" in AppDelegate which is called from "applicationWillTerminate"

What am I doing it wrong ? Why the contents are not saved in core data permanently ?

Bharath
  • 2,064
  • 1
  • 14
  • 39
  • What does your core data stack look like? how many contexts do you have? are you using child and parent contexts? are you using NSPersistent​Container? – Jon Rose Mar 21 '17 at 07:15
  • I have two contexts managedObjectContext & one more child managedObjectContext and not NSPersistentContainer, But I have tried saving using both those contexts and saved both contexts in app delegate save context also, but still I get the same issue. – Bharath Mar 21 '17 at 12:49
  • 1
    It is hard to diagnose without seeing the code, but my guess would be that there is a merge conflict with another context so it can't save to the store. If it is a new project and not hard to change stuff I would recommend using NSPersistentContainer, only write using performBackgroundTask and treat the viewContext as readonly. If it an old project and hard to change the core-data stack I would try to set a merge policy for every context. – Jon Rose Mar 21 '17 at 13:29
  • When, exactly is that code executed? Do you call it yourself or is it only called from `applicationWillTerminate`? – Tom Harrington Mar 21 '17 at 22:50
  • @TomHarrington: I call it myself after updating a model object context & also in applicationWillTerminate. – Bharath Mar 22 '17 at 04:45
  • @All: My issue got resolved while I tried saving using "try managedObjectContext.saveToPersistentStore()". "saveToPersistentStore()" is provided by Restkit. Thanks for your help Friends. – Bharath Mar 22 '17 at 04:47

0 Answers0