Suppose that I have a core data entity ShapesEntry which contains several relationships:
The ShapeInstance entity has three attributes: numSides, edgesLength, color. Inverse to ShapeEntry.
The String_Container entity has one attribute: stringValue. Inverse to ShapeEntry.
The purpose of ShapesEntry is to contain all instances of existing shapes, as well as to keep track of the distinct numSides, edgesLengths, and colors that every recorded shape instance uses.
For example:
^ After adding these shape instances, GameEntry should include each of these four Shape objects, and the NSSet relationships to sideCounts, edgeLengths, and colors should include {3, 4}, {5, 10}, and {RED, GREEN, BLUE}, respectively.
HOWEVER, because String_Container is an object...
String_Container *sides = [NSEntity Description insertNewObjectForEntityForName:@"String_Container" inManagedObjectContext:context];
sides.stringValue = shapeInstance.numSides; //or edgesLength or color
[shapeEntry addSideCounts:sides];
...the above code renders sideCounts, edgeLengths, and colors of {3, 4, 4, 3}, {5, 5, 10, 10}, and {RED, GREEN, BLUE, GREEN} and the addObject method does not recognize them as distinct.
I COULD fetch the appropriate ShapeEntry (this example is dumbed down, so only 1 of these exists here, but in my code you could have thousands), iterate through the existing attributes for each of the relationships and confirm that the value I want to add does not already exist BUT that seems awfully expensive when what I really want is for the relationship to act like an NSSet from the get-go. I do recognize that technically the relationship is doing its job because the objects I want to add are 'distinct objects' despite having identical attribute values, but I would like it to go a step deeper and confirm that these attribute values are distinct before trying to add to the ShapeEntry relationship.
How can I accomplish this task? If my example was confusing, please ask for clarification...I made it up on the spot so hopefully my variable names are consistent...
Thanks in advance!!
Using the Key-Value Coding method
NSSet *colors = [shapeEntry valueForKeyPath:@"shapeInstances.color"];
you get all color values of the shape instances related to shapeEntry
, the same works for the other attributes. This seems to be easier than to duplicate all the information.
You still have the option to "cache" the colors
set in a transient property of ShapeEntry
if necessary.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments