0

I have an iOS app currently in production, with a fair amount of users. There is a functionality that allows user to download some data to be used offline, and I use CoreData to store this data. The data is only stored for up to 29 days maximum.

I am making huge changes to the API, and the way my CoreData data model was set up is completely wrong. I want to start over with a completely new data model, which will be considerably different from the old one, and thus I also want all existing stored data to be discarded, to start freshly without having to worry about a very complicated and long migration.

What is the way to achieve this without risking a crash due to persistent store issues?

el-flor
  • 1,466
  • 3
  • 18
  • 39
  • Create a new database on startup and use that instead of the old one? Remember to include code to check for the existence of the old database so you can delete it once it is no longer needed. – Robotic Cat May 09 '17 at 13:27
  • So would that happen basically where I'm doing this: NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NightlifeModel" withExtension:@"momd"]; ? – el-flor May 09 '17 at 13:30
  • That line looks like you are finding a copy of your compiled `xcdatamodeld` in your bundle (see: http://stackoverflow.com/q/10579767/558933). Normally I would expect to see code similar to this answer (http://stackoverflow.com/a/5633275/558933) to copy a SQLite Core Data database from your bundle to your documents directory. Note that iOS 10 has brought a lot of simplification to setting up Core Data is your app and you should look at the WWDC 2016 videos to see how to take advantage of this. – Robotic Cat May 09 '17 at 15:21

1 Answers1

0

If you don't to write migration policy, then way for you is, delete the old store create the new one in didFinishLaunchingWithOptions and fire initial sync so that whole data is downloaded again for the user. then there won't be any crash.

But it is always preferred to use core data migration and writing migration policy's for heavy migration.

nikBhosale
  • 531
  • 1
  • 5
  • 27