Database Reference
In-Depth Information
In this next section of code, we deal with the possibility that the Ingredient
entity that we need to reference has already been created. Rather than doing
a fetch against the destination context every time, we have a hash built up
and stored within the NSMigrationManger . The NSMigrationManager has an NSDictionary
called userInfo that is perfectly suited for this purpose. We first lazily initialize
this dictionary, and then we lazily initialize another NSDictionary inside it to
store references to the Ingredient entities using the name of the ingredient as
the key. With this, we can make sure that each Ingredient is created only once.
For each Ingredient , we also need to create or reference a UnitOfMeasure . We also
grab a reference to the UnitOfMeasure lookup or create it if it has not been created
yet.
RecipesV3/PPRecipes/RecipeIngredientToIngredient.m
NSManagedObject *dest = [ingredientLookup valueForKey:name];
if (!dest) {
dest = [NSEntityDescription insertNewObjectForEntityForName:destEntityName
inManagedObjectContext:destMOC];
[dest setValue:name forKey:@ "name" ];
[ingredientLookup setValue:dest forKey:name];
name = [source valueForKey:@ "unitOfMeasure" ];
NSManagedObject *uofm = [uofmLookup valueForKey:name];
if (!uofm) {
uofm = [NSEntityDescription insertNewObjectForEntityForName:@ "UnitOfMeasure"
inManagedObjectContext:destMOC];
[uofm setValue:name forKey:@ "name" ];
[dest setValue:uofm forKey:@ "unitOfMeasure" ];
[uofmLookup setValue:uofm forKey:name];
}
}
Next we attempt to locate the Ingredient in the lookup dictionary. If it is not in
the dictionary, we must create it and place it in the dictionary. If we need to
create the Ingredient , we must resolve the UnitOfMeasure as well. Again, if it does
not exist, we create it and put a reference to it in the lookup dictionary.
RecipesV3/PPRecipes/RecipeIngredientToIngredient.m
[manager associateSourceInstance:source
withDestinationInstance:dest
forEntityMapping:mapping];
return YES;
}
- (BOOL)createRelationshipsForDestinationInstance:(NSManagedObject*)dInstance
entityMapping:(NSEntityMapping*)mapping
manager:(NSMigrationManager*)manager
error:(NSError**)error
 
 
 
Search WWH ::




Custom Search