1
votes

Heres the problem... I Have an app in the app store, which uses core data... I have been updating my model correctly (using versions etc) but Just recently I have accidentally lost my latest model (the one that corresponds with the app store) and so now I get this error: reason = "Can't find model for source store";

I have created my model and to the best of my knowledge I have made the changes exactly the same from the previous version... yet I still can't get it to like my model version. So my question is, can I somehow trick core data to thinking it has the right version? or is there a way I can check which entities its having trouble with? I understand that core data stores a hash of the model to check that the versions are the same, but I don't know the extent to which the version models have to be the same in order for the 'hash check' to be successful..

Any help would be great! Thanks!

3
Don't you use a version control tool like say git sow you can retrieve an older version of your data model ?Radu
When creating this new data model did you iterate the version of the data model like the previous one?Radu
yeah, I use git. That was the problem, somehow the new modal version didn't get tracked and so it was deleted when I switched to a different branch. And, yes, I did iterate from the previous versionuser1775671
and i suppose you deleted the content not in the trash and not in the data model pachages?Radu
If there is no way to recover there are a few options: if the data is not important and you don't need to upgrade you can modify the flags passed to the persistent store sow you will not perform lightweight migration, unfortunately the user will lose all of the data stored.Radu

3 Answers

6
votes

You can recover the data model from an app store copy of your app, and import that back into your project. Core Data models don't get compiled in the same way that source code does, so reversing the process is usually effective. The following assumes that you have downloaded the current app store version of the app in iTunes on your Mac:

First copy the app store bundle to a safe place:

cp ~/Music/iTunes/Mobile\ Applications/YOUR-APP-NAME.ipa /tmp
cd /tmp/

Next open up that package, which is really just a zip file.

unzip YOUR-APP-NAME.ipa

This creates a directory called Payload that contains the app and its bundle. The bundle contains the Core Data model. Copy that out of the bundle:

cp -rp Payload/YOUR-APP-NAME.app/YOUR-MODEL-NAME.momd /tmp/

(adjust the name to match your data model).

If you already have more than one version in the app store, the model is a momd that contains multiple mom files. Each mom file corresponds to a model version. One of them is the one you need. You'll need to figure out which is which.

Now, switch to Xcode. Create a new version of the data model but don't make it current. Delete everything in this version, all entities, everything. With the now-empty model displayed, go to the Editor menu and select Import.... In the file open dialog, navigate to the copy of your data model in /tmp/ from above. Select the version you need to recover and click "Open".

All of the entities from that version of the model are now present in the new model file you just created. You can now use this model as the "original" model when doing model migration.

Alternatively, instead of importing into Xcode, you can use my momdec project to decompile the model in place. That will produce an uncompiled Core Data model that you could add to your Xcode project.

5
votes

For future reference and not recommended.

If the model is unrecoverable, you can "fake it".

  1. Create a new model that is identical to the one you lost
  2. Test it and see if you get lucky (unlikely).
  3. Write down the entity hashes that Core Data spits out in the log
  4. Input those hashes into the model as overrides in the (Version Hash Modifier)
  5. Run your migration again.
  6. Never ever modify the production model again. Check it into source control immediately on shipping.
0
votes

For anyone showing up on this question who is not trying to migrate a core data model to another this error can also show up if you are trying to save data to core data that has some values in your entity not being set. Make sure all your data variables are being set and set correctly to avoid this error that dreams of MacBook Pros riding unicorns may live forever in your reality!