1

I'm developing my own app and I have a problem with core data: I submitted my first app at the AppStore and for the next release, I have been doing changes in the Model (new entities has been added and few attributes from existing entities has been removed, added or updated). The mistake that I've done is do not create a new Model Version, I just have made the changes. Now, when I'm using the first version of the app and I change to the second, the app crashes because the Core Data Model has changed. I can't do any migration because the model is not versioned, so I don't have any previous version.

My idea is, when the app is runned for the first time, delete the data model let the app to create it again (there's no problem with data losing).

I have seen many ideas: Stack Overflow 1 Stack Overflow 2

but don't work, because they can't find my file, that's my code:

    let documentsDirectory = URL(string: applicationDirectoryPath())! as NSURL
    // I've tried many names as: Model.sqlite, Database.sqlite, database.sqlite
    let storeURL = documentsDirectory.appendingPathComponent("model.sqlite")
    do
        {
            try FileManager.default.removeItem(at: storeURL!)
        } catch
        {
            print("Problem deleting core data")
        }

How can I find the name of the file to put to this function

appendingPathComponent("I don't know the name of the file")

How can I do coda that? Any other idea?

Thanks for all!

  • Is there a reason that you can't version the CD model for your update? – beyerss May 25 '17 at 11:41
  • Yes it is, I didn't know that I had to do that.. @beyerss – Josep Bernad Espinosa May 25 '17 at 11:42
  • I guess what I'm trying to say is that even though the first release didn't have a version, I *think* you could add a version to the CD model for this new release. Assuming they are small changes, light weight migration would then kick in and you would be able to update without errors. I'm not aware of another way to do this - but I'm, admittedly, not a CoreData expert so if adding a version isn't an option then you may want to wait for someone with more CD experience to chime in. However, you are going to have the same issue with every update if you don't use versioning. – beyerss May 25 '17 at 11:48
  • The problem is that the second releade is in few days, and I've done a lot of commits. How would I do that? – Josep Bernad Espinosa May 25 '17 at 11:50
  • This should not be a long process. I posted an answer with a link to a good tutorial that you should probably look at. Hopefully it will help. – beyerss May 25 '17 at 11:55
  • Does your project use a version control system like git? You could go back and get a copy of the old model and use it to handle migration to your new model. – Tom Harrington May 25 '17 at 15:11

1 Answers1

0

This tutorial includes examples of how to do migration along with the steps for adding a second version of the CD model. If the changes to CD are small then you will not have to do any work to make the update run smoothly. Otherwise, there will be some work required to map the old data structure to the new data structure so that data is not lost.

https://www.raywenderlich.com/145860/core-data-migrations-tutorial-lightweight-migrations-2

beyerss
  • 1,302
  • 3
  • 21
  • 38