Re: Core Data Conundrum
Re: Core Data Conundrum
- Subject: Re: Core Data Conundrum
- From: William Squires <email@hidden>
- Date: Sat, 12 Dec 2009 21:32:30 -0600
Just because core data exists doesn't mean it's the right tool in
all cases. You may have to roll your own.
Have you considered modeling the relationship with a GUID
(globally-unique ID) for each node and segment (assigned in the
class's -(id)init method). Then you could model the nodes and
segments using only entities (integers). Then write your own
controller class (perhaps by wrapping an NSArrayController) that'll
load and save the objects and perform the work of linking them
together at runtime.
Here's how I imagine I would solve this:
Start with two model classes
#import "Node.h"
@class Segment;
@interface Node : NSObject
{
NSMutableArray *segments;
NSMutableArray *segmentGUIDs;
// ... whatever other data you need a node to hold
int myGUID;
}
-(id)initWithPremadeGUID:(int)guid;
// Other methods as needed
@end
and
#import "Segment.h"
@class Node;
@interface Segment : NSObject
{
Node *node1;
Node *node2;
int node1GUID;
int node2GUID;
int myGUID;
// Whatever other data you need a segment to hold
}
-(id)initWithPremadeGUID:(id)guid;
// Other methods as needed
@end
In your Node.m file:
#import "Node.h"
#import "Segment.h"
@implementation Node
-(id)init
{
if (self = [super init])
{
myGUID = MyGetGUID();
segments = [[NSArray alloc] init];
// Whatever other initialization you need to do
}
return self;
}
-(id)initWithPremadeGUID:(id)guid
{
if (self = [super init])
{
myGUID = guid;
segments = [[NSArray alloc] init];
// Whatever other initailization you need to do
}
return self;
}
and similarly in your Segment.m file.
You would also have a method that would save the data to the file
(in a controller class). When you call the method to load the file
(also in your controller), it'll use the -(id)initWithPremadeGUID:
method using the guid info from the file stream, rather than asking
the MyGetGUID() C function. This way the object - Node or Segment -
will have the same GUID it had when saved, and the information
content will be preserved.
This (saving and loading) would occur in your controller class,
and - upon loading - would hook up all the Nodes and Segments using
the GUIDs (by loading the segmentGUIDs NSMutableArray or the
node1GUID/node2GUID properties in the respective model classes.)
This should allow you to get around the many-to-many relationship
you would otherwise have to create in Core Data (or a relational DBMS).
}
On Dec 12, 2009, at 6: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.
Any suggestions?
TIA,
Rick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
40satx.rr.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden