Core data migration failing with “Can't find model for source store” but managedObjectModel for source is present

怎甘沉沦 提交于 2019-12-03 06:38:36

I'm answering my own question here in case it helps somebody.

The crucial problem is that, when I reached version 4 of my object model, I also added an additional managed object model to the project. This additional model was separate from my main model, and is just used to create a cache on another thread and contains data that is unrelated to the main model.

Foolishly I still initialized my managedObjectModel using

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]

which in my case created a model containing entities from my main model as well as my other model. These unwanted entities had their version hashes in my database. When core-data then goes to look for a managedobjectmodel that matches all these hashes it naturally fails to find it.

In my case the solution was to manually clean my db files prior to migration (removing versionhashes from unwanted entities) .. and then to change my managedObjectModel loading code to;

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyDataModel" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!