【发布时间】:2011-04-17 00:50:05
【问题描述】:
我有一个使用 CoreData 的 iPhone 应用程序。我最近对数据模型进行了一些小改动,现在每次打开应用程序时都会收到错误消息“找不到源存储模型”。
我有 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....>。
【问题讨论】:
标签: ios iphone cocoa-touch core-data migration