Database Reference
In-Depth Information
Next, we must deal with the relationships. Just as with the attributes, we
can find out the name of each relationship from the NSEntityDescription . By
looping over the resulting NSDictionary , we can process each relationship.
However, to make sure we are not in an infinite recursion, we check each
relationship to see whether we should be following it. If we shouldn't follow
it, we simply skip to the next relationship.
Baseline/PPRecipes/PPRExportOperation.m
if ([relDesc isToMany]) {
NSMutableArray *array = [NSMutableArray array];
for (NSManagedObject *childMO in [mo valueForKey:key]) {
[array addObject:[self moToDictionary:childMO]];
}
[dict setValue:array forKey:key];
continue ;
}
NSManagedObject *childMO = [mo valueForKey:key];
[dict addEntriesFromDictionary:[self moToDictionary:childMO]];
}
return dict;
}
From the perspective of the current NSManagedObject , there are two types of
relationships: to-one or to-many. If it is a to-one relationship, we grab the
object at the other end of the relationship and turn it into an NSDictionary for
inclusion in our NSMutableDictionary .
If the relationship is a to-many, we need to iterate over the resulting set and
turn each NSManagedObject into an NSDictionary and include it in a temporary
array. Once we have iterated over all of the objects in the relationship, we
add the entire array to our master NSMutableDictionary using the relationship
name as the key.
Once all of the relationships have been processed, we return the NSMutableDic-
tionary to the -main so that we can complete the final step.
Baseline/PPRecipes/PPRExportOperation.m
NSError *error = nil;
NSDictionary *objectStructure = [self moToDictionary:localRecipe];
NSData *data = [NSJSONSerialization dataWithJSONObject:objectStructure
options:0
error:&error];
[self completionBlock](data, error);
}
At the end of the -main , we use the NSJSONSerialization class to turn the NSDictionary
structure into a JSON structure. Note that the PPRExportOperation does not care
 
 
 
Search WWH ::




Custom Search