Database Reference
In-Depth Information
Making the Copy One-Way
When performing this copy, it's easy to accidentally copy the entire Core Data
repository. Because all our objects are linked via two-way relationships, if we built a
recursive method to copy the objects and follow their relationships, we would end up
with a complete duplicate of all the recipes.
To prevent this, we added a check into each relationship copy. Whenever it follows a
relationship, it first checks to make sure that the destination entity should be copied.
We do this with a key-value pair in the data model. If there's a key called
ppExportRela-
tionship
and that key has a value of
NO
, we skip the relationship. By taking this step,
we guarantee the entity tree is copied in only one direction, as shown in
Figure 16,
The flow of the copy
, on page 85
.
Recipe
Author
RecipeIngredient
UnitOfMeasure
Ingredient
Figure 16—The flow of the copy
NSDictionary *relationships = [entity relationshipsByName];
NSRelationshipDescription *relDesc = nil;
for
(NSString *key in relationships) {
relDesc = [relationships objectForKey:key];
if
(![[[relDesc userInfo] valueForKey:ppExportRelationship] boolValue]) {
DLog(@
"Skipping %@"
, [relDesc name]);
continue
;
}