自定义迁移过程
如果你像自己启动迁移,你只需要自定义迁移过程即可。你可以这样做,例如,在应用程序的主包意外的位置搜索模型,或者通过使用不同的映射模型执行多次传递来处理大型数据集(详情可以参阅Mtltiple Passes - Dealing With Large Detasets)。
是否需要迁移(Is Migration Necessary)
在启动迁移过程之前,你应首先确定是否需要迁移。你可以检查 NSManagedObjectModel的isConfiguration:CompatibleWithStoreMetadata:如7 -1所示:
Listing 7-1 Checking whether migration is necessary
NSPersistentStoreCoordinator *psc = /* get a coordinator */ ;
NSString *sourceStoreType = /* type for the source store, or nil if not known */ ;
NSURL *sourceStoreURL = /* URL for the source store */ ;
NSError *error = nil;
NSDictionary *sourceMetadata =
[NSPersistentStoreCoordinator metadataForPersistentStoreOfType:sourceStoreType
URL:sourceStoreURL
error:&error];
if (sourceMetadata == nil) {
// deal with error
}
NSString *configuration = /* name of configuration, or nil */ ;
NSManagedObjectModel *destinationModel = [psc managedObjectModel];
BOOL pscCompatibile = [destinationModel
isConfiguration:configuration
compatibleWithStoreMetadata:sourceMetadata];
if (pscCompatibile) {
// no need to migrate
}
初始化迁移管理器
使用initWithSourceModel: destinationModel: 的方法初始化迁移管理器;因此首先需要找到合适的储存模型。使用NSManagedObjectModel的ModelFromBundles:forStoreMetadata:的方法获得储存模型。如果返回一个合适的模型,那么就可以创建迁移管理器。如7 - 2所示:
Listing 7-2 Initializing a Migration Manager
NSArray *bundlesForSourceModel = /* an array of bundles, or nil for the main bundle */ ;
NSManagedObjectModel *sourceModel =
[NSManagedObjectModel mergedModelFromBundles:bundlesForSourceModel
forStoreMetadata:sourceMetadata];
if (sourceModel == nil) {
// deal with error
}
MyMigrationManager *migrationManager =
[[MyMigrationManager alloc]
initWithSourceModel:sourceModel
destinationModel:destinationModel];
进行迁移
迁移储存使用NSMigrationManager的migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:的方法。要使用这个方法,需要编组以些参数;大多数是直接的,唯一需要一些工作的是发现适当的映射模型(可以使用NSMappingModels的mappingModelFromBundles:forSourceModel:destinationModel:方法)。如 7- 3所示。(接上7 - 2)。
Listing 7-3 Performing a Migration
NSArray *bundlesForMappingModel = /* an array of bundles, or nil for the main bundle */ ;
NSError *error = nil;
NSMappingModel *mappingModel =
[NSMappingModel
mappingModelFromBundles:bundlesForMappingModel
forSourceModel:sourceModel
destinationModel:destinationModel];
if (mappingModel == nil) {
// deal with the error
}
NSDictionary *sourceStoreOptions = /* options for the source store */ ;
NSURL *destinationStoreURL = /* URL for the destination store */ ;
NSString *destinationStoreType = /* type for the destination store */ ;
NSDictionary *destinationStoreOptions = /* options for the destination store */ ;
BOOL ok = [migrationManager migrateStoreFromURL:sourceStoreURL
type:sourceStoreType
options:sourceStoreOptions
withMappingModel:mappingModel
toDestinationURL:destinationStoreURL
destinationType:destinationStoreType
destinationOptions:destinationStoreOptions
error:&error];
Multiple Passes - Dealing With Large Datasets
上面所示的基本方法是让迁移管理器使用两个数据模型,然后迭代映射模型中提供的步骤(映射),以将数据从一侧移动到下一个(另一侧)。 因为Core Data执行“三阶段”迁移。其中它首先创建所有数据,然后在第二阶中段关联数据 - 它必须维护“关联表”(它指出目标储存中的哪个对象是被迁移的源代码储存中的对象的版本,反之亦然),此外,由于它没有一中方法来刷新正在使用的上下文,这意味着将在迁移过程中,迁移管理器中会积累许多对象。
为了解决这个问题,映射模型作为参数,在migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptiuons:error:方法中调用本身。这意味着如果你可以隔离图形(graph)的一部分(就映射而言),并在单独的映射模型中创建他们,则可以执行一下操作:
- 获取源和目标数据模型
- 与他们创建一个迁移管理器
- 查找所有映射模型,并将他们放到数组中(如果需要的话,按照一定顺序排列)
- 循环遍历数组,并调用migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destiantionOptions:error的方法用于每个映射。
这允许你一次迁移数据的一个“块”,而不是立即拉入所有的数据。从“跟踪/显示进度”的角度来看,这基本上只是创造另一层工作,所以你可以根据迭代的映射模型的数量来确定完成的百分比。
网友评论