我在使用 Xcode3 时从来没有遇到过任何问题,但是在使用 Xcode4 时,当我更新核心数据模型时,Apple 的代码大约 3 次失败,出现可怕的“持久存储迁移失败,缺少源管理对象模型”。错误。
这是我的设置(我如何将项目配置为自动迁移):
- NSPersistentDocument,来自 Apple 的模板
- 覆盖 Apple 的模型加载方法,我唯一要做的就是在 storeOptions 字典中提供两个标志,它们会打开自动迁移
-(BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error
{
NSMutableDictionary *newOptions = nil;
if( storeOptions != nil )
newOptions = [NSMutableDictionary dictionaryWithDictionary:storeOptions];
else
newOptions = [NSMutableDictionary dictionary];
[newOptions setValue:@"YES" forKey:NSMigratePersistentStoresAutomaticallyOption];
[newOptions setValue:@"TRUE" forKey:NSInferMappingModelAutomaticallyOption];
BOOL success = FALSE;
success = [super configurePersistentStoreCoordinatorForURL:url ofType:fileType modelConfiguration:configuration storeOptions:newOptions error:error];
return success;
}
这是我一直在使用的过程(它已经解决了 Xcode4 中的 1 个错误!)
- 选择模型(在 Xcode4 中命名为“something.xcdatamodel”,左侧有一个twisty)
- 转到编辑器菜单,选择“添加新模型版本...”
- 将新版本命名为比上一个大的 1 整数 - 例如,如果以前是“4”,则将新版本命名为“5”
- 在右侧窗格中,将当前模型版本更改为新创建的版本
- XCode4 错误的解决方法:选择任何文件,然后选择新创建的模型。如果不这样做,Xcode 会在新创建的模型上显示选择,但会改为编辑以前的模型,这肯定会破坏 CoreData 中的所有内容
- 编辑您的模型;在这种情况下,我正在向现有实体添加一个新属性
- 节省。建造。跑。... 碰撞。
除了,正如我所说,大约 3 次中的 2 次可以正常工作。一旦它工作一次,它(显然)很好 - 轻量级迁移完成,下一次保存保存在新模型版本中。
所以我猜我在上述步骤中做错了什么,但我已经通过文档 5 或 6 次并且看不到任何明显的东西。NSPersistentDocument 文档都已过时并无济于事 - 但我也多次在 iPhone 上进行过轻量级迁移,所以我对这样做有相当的信心,这对我来说似乎是正确的。
我尝试过/检查过的其他事情:-iPhone Core Data Lightweight Migration Cocoa 错误 134130:找不到源存储的模型(不;只包含根 xcdatamodel)