Search code examples
xcodecore-dataswift-playgroundxcdatamodel

Xcode Core Data models are missing


I am working on an iOS app using Core Data, and after doing some stuff in the storyboard, I tried to run the app and realize that all of my Core Data models are missing except my original one. My playground is also missing, but I don't really care about that. I have no idea what happened, and really need to get those data models back. This is what I see when I look for them in the project navigator.

enter image description here

When I try to build the app, it builds fine, but then I immediatly get this error:

016-02-12 11:46:22.779 App Name[51990:1666336] CoreData: Failed to load NSManagedObjectModel with URL 
'file:///Users/myname/Library/Developer/CoreSimulator/Devices/EC2BDB71-BE0C-46CB-B208-09C5D0FD2F3B/data/Containers/Bundle/Application/76FAF1C3-8F85-4A1A-81DE-309B984176E1/App%20Name.app/DataModel.momd/'
fatal error: unexpectedly found nil while unwrapping an Optional value

I tried undoing all my changes I made in git since the last commit, but it still can't find them. What could have happened? Is there any way I can get them back?


Solution

  • It's hard to say exactly what happened, but something is causing your files to disappear. It's not in any way related to Core Data or Swift playgrounds-- you've got a serious problem somewhere and it's making you lose data.

    These files should be in your git repository. If you can't find them by undoing changes since your last commit, you need to dig deeper. If they were ever committed, they're recoverable, but going back to the last commit may not be enough. They should also exist in whatever system you use to back up your hard drive.

    Core Data may be able to help you recover the models by other means. If you have an older compiled copy of the app somewhere, it will contain compiled versions of the data model files. Xcode can import those and re-create the old model files. You'd do this by creating a new model file, then going to the "Edit" menu and selecting "Import...".

    But, all this is secondary to your real problem, which is that you're losing files and you don't know why. You could lose more files in the future. Whatever happened could happen again. You need to figure out why you're losing files and fix that problem.