Database Reference
In-Depth Information
the source model and the target and return NO . At this point, we should have
all the components we need for one migration. The source model, target
model, and mapping model are all known quantities. Now it's time to migrate!
Performing the Migration
In this block, we are instantiating an NSMigrationManager (if we needed something
special, we would build our own manager) with the source model and the desti-
nation model. We are also building up a unique path to migrate to. In this
example, we are using the destination model's filename as the unique change to
the source store's path. Once the destination path is built, we then tell the
migration manager to perform the migration and check to see whether it was
successful. If it wasn't, we simply return NO because the NSError will be populated
by the NSMigrationManager . If it's successful, there are only three things left to do:
move the source out of the way, replace it with the new destination store, and
finally recurse.
RecipesV3/PPRecipes/PPRAppDelegate.m
NSMigrationManager *manager = [[NSMigrationManager alloc]
initWithSourceModel:sourceModel
destinationModel:targetModel];
NSString *modelName = [[modelPath lastPathComponent]
stringByDeletingPathExtension];
NSString *storeExtension = [[sourceStoreURL path] pathExtension];
NSString *storePath = [[sourceStoreURL path] stringByDeletingPathExtension];
//Build a path to write the new store
storePath = [NSString stringWithFormat:@ "%@.%@.%@" , storePath,
modelName, storeExtension];
NSURL *destinationStoreURL = [NSURL fileURLWithPath:storePath];
if (![manager migrateStoreFromURL:sourceStoreURL
type:type
options:nil
withMappingModel:mappingModel
toDestinationURL:destinationStoreURL
destinationType:type
destinationOptions:nil
error:error]) {
return NO;
}
In this final code block, we first create a permanent location for the original
store to be moved to. In this case, we will use a globally unique string gener-
ated from the NSProcessInfo class and attach the destination model's filename
and the store's extension to it. Once that path is built, we move the source
to it and then replace the source with the destination. At this point, we are
at the same spot we were when we began except that we are now one version
closer to the current model version.
 
 
 
Search WWH ::




Custom Search