自动核心数据迁移在Mac OS X 10.5上失败,但在10.6或10.7上失败

And*_*sen 9 macos cocoa osx-leopard core-data objective-c

我有一个基于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];

}
Run Code Online (Sandbox Code Playgroud)

这在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."
Run Code Online (Sandbox Code Playgroud)

如果我改为手动启动迁移,请使用以下代码:

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;
Run Code Online (Sandbox Code Playgroud)

迁移工作正常.但是,我更喜欢使用自动迁移,如果没有其他原因,因为它使代码更清晰.有没有人看到类似的自动迁移问题,适用于10.6+但不适用于10.5?我的预感是,它很简单,就像内置的迁移代码由于某种原因无法找到映射模型,但我无法弄清楚它必须是什么.

mac*_*dyp 1

我并不是 100% 这与您的问题有关,但 Apple 有一个记录的解决方法,说明如何迁移 10.6 的 Core Data 模型,该模型也必须与 10.5 兼容。10.5 中似乎缺少 10.6 所依赖的迁移方法。

destinationInstancesForSourceRelationshipNamed:sourceInstances:

是缺少的方法。

希望这有帮助。

参考:http://developer.apple.com/library/mac/#/legacy/mac/library/releasenotes/Cocoa/MigrationCrashBuild106Run105/_index.html