繁体   English   中英

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

[英]Automatic Core Data migration fails on Mac OS X 10.5 but not on 10.6 or 10.7

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

是缺少的方法。

希望这会有所帮助。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM