核心数据迁移失败,并显示“无法找到源存储的模型”但是managedObjectModel是存在的 [英] Core data migration failing with "Can't find model for source store" but managedObjectModel for source is present

查看:147
本文介绍了核心数据迁移失败,并显示“无法找到源存储的模型”但是managedObjectModel是存在的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可可应用程序使用核心数据,现在在其管理对象模型的第4版。



我的托管对象模型包含抽象实体,但到目前为止,我已经设法通过创建适当的映射模型并使用addPersistentStoreWithType创建持久存储来获得迁移:configuration:options:error并且NSMigratePersistentStoresAutomaticallyOption设置为YES。

  NSDictionary * optionsDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption] ; 
NSURL * url = [NSURL fileURLWithPath:[applicationSupportFolder stringByAppendingPathComponent:@MyApp.xml]];
NSError * error = nil;
[theCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:optionsDictionary error:& error]

当我从模型版本3迁移到4,这是一个很好的迁移,这是一个涉及添加属性到几个实体的迁移。现在,当我尝试添加一个新的模型版本(版本5),调用addPersistentStoreWithType返回nil,错误仍然为空。从4到5的迁移涉及添加单个属性。



我很难调试问题并检查所有以下内容:


  1. 源数据库实际上是版本4,persistentStoreCoordinator的管理对象模型是版本5。


  2. 4-> 5映射模型


  3. 我尝试了各种模型升级路径。奇怪的是,我发现从早期版本3 - > 5升级,但从4 - > 5升级失败。


  4. 实体迁移策略,用于迁移其属性正在改变的实体...在这种情况下,我覆盖了方法beginEntityMapping:manager:error:。有趣的是,当迁移工作时(即当我从3迁移到4,或从3迁移到5)时,该方法会被调用,但是在失败(4到5)的情况下它不会被调用。


我对于处理进度有点失落。任何想法,以帮助调试这个问题将不胜感激。

解决方案

我在这里回答我自己的问题,以防它帮助别人。



关键的问题是,当我到达我的对象模型的第4版时,我还为项目添加了一个额外的托管对象模型。这个附加模型与我的主模型是分开的,只是用来在另一个线程上创建一个缓存,并且包含与主模型无关的数据。



使用

初始化我的managedObjectModel

  managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain] 



在我的示例中创建了一个模型,其中包含来自我的主模型以及我的其他模型的实体。这些不需要的实体在我的数据库中有它们的版本哈希。当核心数据然后去寻找一个匹配所有这些哈希值的managedobjectmodel时,它自然找不到它。



在我的情况下,解决方案是手动清理我的数据库文件然后将我的managedObjectModel加载代码更改为

  NSString * path = [[NSBundle mainBundle] pathForResource:@MyDataModelofType:@momd]; 
NSURL * momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];


I have a cocoa application using core-data, which is now at the 4th version of its managed object model.

My managed object model contains abstract entities but so far I have managed to get migration working by creating appropriate mapping models and creating my persistent store using addPersistentStoreWithType:configuration:options:error and with the NSMigratePersistentStoresAutomaticallyOption set to YES.

NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
NSURL *url = [NSURL fileURLWithPath: [applicationSupportFolder stringByAppendingPathComponent: @"MyApp.xml"]];
NSError *error=nil;
[theCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:optionsDictionary error:&error]

This works fine when I migrate from model version 3 to 4, which is a migration that involves adding attributes to several entities. Now when I try to add a new model version (version 5), the call to addPersistentStoreWithType returns nil and the error remains empty. The migration from 4 to 5 involves adding a single attribute.

I am struggling to debug the problem and have checked all the following;

  1. The source database is in fact at version 4 and the persistentStoreCoordinator's managed object model is at version 5.

  2. The 4->5 mapping model as well as managed object models for versions 4 and 5 are present in the resources folder of my built application.

  3. I've tried various model upgrade paths. Strangely I find that upgrading from an early version 3 -> 5 works .. but upgrading from 4 -> 5 fails.

  4. I've tried adding a custom entity migration policy for migration of the entity whose attributes are changing ... in this case I overrode the method beginEntityMapping:manager:error: . Interestingly this method does get called when migration works (ie when I migrate from 3 to 4, or from 3 to 5 ), but it does not get called in the case that fails ( 4 to 5 ).

I'm pretty much at a loss as to where to proceed. Any ideas to help debug this problem would be much appreciated.

解决方案

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];

这篇关于核心数据迁移失败,并显示“无法找到源存储的模型”但是managedObjectModel是存在的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆