我添加了一个新的模型版本,并将核心数据模型设置为使用该新版本,但当应用程序尝试启动时出现此错误。
“用于打开持久存储的托管对象模型版本与用于创建持久存储的版本不兼容。”
![enter image description here](https://i.stack.imgur.com/w2umq.png)
我猜问题是当前的持久存储是模型的旧版本。有没有办法直接删除它,然后再创建一个新的?我不关心保存任何数据。
您必须在版本之间迁移。根据Apple的文档,如果更改很简单,则可以进行轻量级迁移。
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweight.html#//apple_ref/doc/uid/TP40008426-SW1
将这些选项添加到 NSPersistentStoreCoordinator 似乎有效。
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"YOURAPP.storedata"];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:options error:&error]) {
[[NSApplication sharedApplication] presentError:error];
[persistentStoreCoordinator release], persistentStoreCoordinator = nil;
return nil;
}
return persistentStoreCoordinator;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)