0

I saved Core data context successfully. when I deleted application instance(only instance, not application itself), all saved managed objects at core data context are also deleted. But i want to persist these objects. Here is the snapshot of my code. When I run application for first time, this code writes 'aaban' to core data context. and saves it.

Now for second time, this code prints 'aaban', if i don't delete application instance(not application itself). But If i delete application instance (not application itself), this will not print 'aaban'.

enter image description here

  • context.save() is called ? – Prashant Tukadiya Jul 23 '18 at 12:42
  • yes. let me share that code also. func applicationDidEnterBackground(_ application: UIApplication) { self.saveContext() } – Aaban Tariq Murtaza Jul 23 '18 at 12:45
  • @aaban-tariq-murtaza : Typically you dont wait till application enter background to save data to persistent store, rather as and when you think you have stable data you update your core data. We can not help you with method name like `self.saveContext()` we dont know what have you implemented inside it, so please post the code for that so that we can help you further – Sandeep Bhandari Jul 23 '18 at 12:50
  • Whenever i create one managed instance. I use to save it also. see this code\n let city = City(context: SharedData.shared().managedContext!)\n city.id = (item[CONSTANTS.JSON_KEYS.LIST_HOME_CITY_ID] as! String\n SharedData.shared().managedContext?.save() – Aaban Tariq Murtaza Jul 23 '18 at 12:52
  • even i also checked the underlying sqlite, sqlite also have the saved data. but when i deleted Application instance(Not application itself), this sqlite data also removed. – Aaban Tariq Murtaza Jul 23 '18 at 13:04
  • How the simulator handles application data directories in later versions of iOS is explained well in https://stackoverflow.com/a/29986807/6875565. – particleman Jul 23 '18 at 14:48

1 Answers1

0

If you have to, store just relative URLs and not absolute ones, since iOS 8 Absolute urls to app's sandbox will change every time application is relaunched. That is why you see BAF03763-B294-4EBB-AB9B-06B29B8EDB80 be changed to C7669201-1606-4596-B420-87A543569DDD.

You should just get the path to your sqlite store every time the app is relaunched.

Ladislav
  • 6,767
  • 5
  • 24
  • 29