我有一个使用CoreData的iPhone应用程序。我最近对数据模型做了一些小改动,现在每次打开应用程序时,都会出现“无法找到源存储模型”的错误。iPhone CoreData迁移失败,“无法找到源存储模型”
我有2个版本的数据模型,我做的唯一更改是一些字段的添加。我按照最初工作的指导here,然后在今天,添加了一些额外的领域后,它打破了。所有附加字段都标记为可选,并且都具有默认值。迁移代码如下:这里创建成功
NSURL *storeUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"xxx.sqlite"]];
// migration options
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
...
}
的managedObjectModel:
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"DataModelName" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return managedObjectModel;
}
我的版本问题追查到的失配有1个实体。获取引发错误包括这对实体:
MyEntityName = <cc1456b7 b12d0d05 21930308 94ccc078 27a6c345 8847c738 e3a9ae7e 0be9535d>;
,但在应用程序捆绑在VersionInfo.plist哈希是:
MyEntityName = <fede6b59 462442d1 8fc98226 b9f8f745 3250dabd ee188248 cb97b1d0 8a74eef3>;
有在任何地方VERSIONINFO没有其他实体。 plist与散列<cc1456b7....>
。
我确实在运行应用程序后编辑了第2版。什么是继续这个最好的方式?我绝对不希望将其发布到生产环境中,并让用户丢失数据,因为我在开发过程中搞砸了。我可以创建最终编辑的第3版,并且可以继续吗? – 2010-09-25 20:22:24
如果您还没有向用户发布第2版,那么他们不会有任何问题。您遇到问题是因为您保存了“旧版本2”,并且正在尝试将其加载到“新版本2”。不过,添加新的不必要的模型版本并不会造成伤害它只是让你的应用程序更大一点。 – 2010-09-26 11:53:41
非常好。我有些担心,因为我们的一些内部测试人员出现错误,但可能是由于我在运行带有多个更改的版本时遇到的相同问题。 – 2010-09-27 05:13:28