我有一个基于 NSPersistentDocument 的核心数据应用程序,它面向 10.5 Leopard 及更高版本。我即将发布对数据模型进行更改的更新,因此需要将现有文档迁移到新模型。这些更改相对简单,我已经为它们创建了一个映射模型。请注意,我并没有尝试进行自动轻量级迁移,我确实有一个映射模型(Leopard 不支持轻量级迁移,但我的模型更改无论如何都排除了它)。在我的 NSPersistentDocument 子类中,我覆盖了 -configurePersistentStoreCoordinatorForURL...
如下:
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url
ofType:(NSString *)fileType
modelConfiguration:(NSString *)configuration
storeOptions:(NSDictionary *)storeOptions
error:(NSError **)error
{
NSMutableDictionary *newOptions = (storeOptions ?
[NSMutableDictionary dictionaryWithDictionary:storeOptions] :
[NSMutableDictionary dictionary]);
[newOptions setObject:(id)kCFBooleanTrue forKey:NSMigratePersistentStoresAutomaticallyOption];
return [super configurePersistentStoreCoordinatorForURL:url
ofType:fileType
modelConfiguration:configuration
storeOptions:newOptions
error:error];
}
这在 10.6 和 10.7 上运行良好。但是,在 10.5 上,对 [super configurePersistentStore...]
的调用抛出异常并失败。错误是:
Error Domain=NSCocoaErrorDomain Code=134020 UserInfo=0x15812d70 "The model configuration used to open the store is incompatible with the one that was used to create the store."
如果我使用以下代码手动启动迁移:
NSArray *bundles = [NSArray arrayWithObject:[NSBundle mainBundle]];
NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:bundles forStoreMetadata:sourceMetadata];
NSManagedObjectModel *destinationModel = [psc managedObjectModel];
NSMappingModel *mappingModel = [NSMappingModel mappingModelFromBundles:bundles forSourceModel:sourceModel destinationModel:destinationModel];
NSMigrationManager *migrationManager = [[[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel] autorelease];
BOOL migrationSuccessful = [migrationManager migrateStoreFromURL:backupURL
type:NSXMLStoreType
options:storeOptions
withMappingModel:mappingModel
toDestinationURL:url
destinationType:NSXMLStoreType
destinationOptions:storeOptions
error:error];
return [psc addPersistentStoreWithType:NSXMLStoreType configuration:configuration URL:url options:storeOptions error:error] != nil;
迁移工作正常。但是,我更愿意使用自动迁移,如果没有其他原因,只是因为它可以使代码更清晰。有没有人看到类似的自动迁移问题,适用于 10.6+ 但不适用于 10.5?我的直觉是,这是相当简单的事情,例如内置迁移代码由于某种原因找不到映射模型,但我无法弄清楚它一定是什么。
最佳答案
我不是 100% 这与您的问题有关,但 Apple 有一个记录的解决方法,说明如何迁移 10.6 的核心数据模型,该模型也必须与 10.5 兼容。 10.5 中似乎缺少 10.6 迁移所依赖的方法。
destinationInstancesForSourceRelationshipNamed:sourceInstances:
是缺少的方法。
希望这对您有所帮助。
关于objective-c - 自动核心数据迁移在 Mac OS X 10.5 上失败,但在 10.6 或 10.7 上则不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791706/