Database Reference
In-Depth Information
createDestinationInstancesForSourceInstance:
The first method,
createDestinationInstancesForSourceInstance:
, is called for each
entity in the source store that is associated with this migration policy. For
example, during the migration of the
RecipeIngredient
entities and the creation
of the
Ingredient
and
UnitOfMeasure
entities, this method would be called for each
RecipeIngredient
, and it would be expected that at least an
Ingredient
entity would
be created or associated with the incoming
RecipeIngredient
as a result.
The code to implement this breaks down as follows:
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject*)source
entityMapping:(NSEntityMapping*)mapping
manager:(NSMigrationManager*)manager
error:(NSError**)error
{
NSManagedObjectContext *destMOC = [manager destinationContext];
NSString *destEntityName = [mapping destinationEntityName];
NSString *name = [source valueForKey:@
"name"
];
In the first part of the method, we are simply setting up references that will
be needed later. Specifically, we are getting a reference to the destination
NSManagedObjectContext
, which we will need to create new entities, the name of
the destination entity, and, most importantly, the
name
value from the incoming
entity.
Since the incoming entity is a
RecipeIngredient
, the
name
value will be the name
of the ingredient that we now want to reference.
NSMutableDictionary *userInfo = (NSMutableDictionary*)[manager userInfo];
if
(!userInfo) {
userInfo = [NSMutableDictionary dictionary];
[manager setUserInfo:userInfo];
}
NSMutableDictionary *ingredientLookup = [userInfo valueForKey:@
"ingredients"
];
if
(!ingredientLookup) {
ingredientLookup = [NSMutableDictionary dictionary];
[userInfo setValue:ingredientLookup forKey:@
"ingredients"
];
}
NSMutableDictionary *uofmLookup = [userInfo valueForKey:@
"unitOfMeasure"
];
if
(!uofmLookup) {
uofmLookup = [NSMutableDictionary dictionary];
[userInfo setValue:uofmLookup forKey:@
"unitOfMeasure"
];
}