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

时间:2012-03-20 17:16:55

标签: objective-c macos cocoa core-data osx-leopard

我有一个基于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?我的预感是,它很简单,就像内置的迁移代码由于某种原因无法找到映射模型,但我无法弄清楚它必须是什么。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

NSPersistentStore是罪魁祸首。它直到10.6才实现migrationManagerClass

http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSPersistentStore_Class/NSPersistentStore.html

Apple的解决方法(正如您已经使用过的)是自己创建迁移管理器,而不是依靠NSPersistentStore来提供它。

相关问题