On Dec 12, 2009, at 4:30 PM, Rick Mann wrote:
> Hi. I'm trying to set up a model for a Node and a Segment. A Segment exists
> between Nodes. Each Segment has a node1 and node2 attribute, and each Node
> can have multiple Segments. I don't seem to be able to model this
> relationship; I can have either node1 with an inverse of segments, or node 2,
> but not both.
>
> It's important for me to distinguish which end of a segment a particular Node
> is associated with (imagine drawing a directed arrow). I feel like this
> should be straightforward, but I'm stuck.
Do you actually need to be able to work with all segments of a Node in fetch
requests? If you don’t need to work with all of a Node’s segment within a
fetch request, you can actually make that a code-level property on your
NSManagedObject subclass and just have “incomingSegments” and
“outgoingSegments” relationships on your Node:
entity Node {
relationship incomingSegments
destination: Segment,
cardinality: to-many,
inverse: Segment.destinationNode;
relationship outgoingSegments
destination: Segment,
cardinality: to-many,
inverse: Segment.sourceNode;
};
entity Segment {
relationship sourceNode
destination: Node,
cardinality: to-one,
inverse: Node.outgoingSegments;
relationship destinationNode
destination: Node,
cardinality: to-one,
inverse: Node.incomingSegments;
};
Then in your NSManagedObject subclass representing a Node, just in case you
need to do something like iterate over all related Segment instances to render
each one in a view:
@interface MyNode : NSManagedObject
@property (readonly, copy) NSSet *allSegments; // non-modeled property,
don’t query on it
@end
@implementation MyNode
- (NSSet *)allSegments {
return [self.incomingSegments
setByAddingObjectsFromSet:self.outgoingSegments];
}
@end
You may also be able to implement allSegments as a fetched property, leaving
its implementation up to Core Data. It still wouldn’t be a property you could
use in your own fetch requests though.
— Chris
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]