Re: Core Data: Instantiating linked entities
Re: Core Data: Instantiating linked entities
- Subject: Re: Core Data: Instantiating linked entities
- From: Quincey Morris <email@hidden>
- Date: Mon, 1 Sep 2008 10:10:30 -0700
On Sep 1, 2008, at 03:15, Renaud Céroce wrote:
An NSArrayController resides in the document's nib. It is configured
to
contain objects of the ShapeManagedObject class.
I read somewhere in Apple's doc that an object was responsible for
instantiating the other objects it depends on. It don't find it
logical
because I've already defined the relationships in the xcdatamodel, but
anyway, I add the Color and Rect to the graph in the -awakeFromInsert
method of ShapeManagedObject:
- (void) awakeFromInsert
{
[super awakeFromInsert];
_rect = [NSEntityDescription insertNewObjectForEntityForName:@"Rect"
inManagedObjectContext:[self managedObjectContext]];
[_rect retain];
_color = [NSEntityDescription
insertNewObjectForEntityForName:@"Color"
inManagedObjectContext:[self managedObjectContext]];
[_color retain];
}
_rect and _color are two instance variables of type NSManagedObject*.
Please note the [retain]s, as both objects are autoreleased.
Up to this point, it works as expected, the shape does display with
the
default colour and at the right location. I can change the colour
and rect
which changes the display accordingly.
I now save my document (its content is OK, I verified the XML), and
open it
again. Now, the colour is wrong because the 'color' key equals nil. It
seems the Color was not added to the graph.
My first idea was to implement the same code in -awakeFromFetch, but
it
obviously does not work, because it inserts Color and Rect objects
with
default values.
Now, my questions:
1) What am I doing wrong ? Am I even to insert related objects using
insertNewObjectForEntityForName:inManagedObjectContext: like I do ?
2) Imagine the nib contains two NSArrayControllers which both
maintains the
same class (ShapeManagedObject in this example). How can CoreData
decide in
which one to add new shapes ?
You're mixing up two entirely different issues here.
1. You should NOT define instance variables for the Rect and Color
objects. The whole point of Core Data is that it provides the data
storage for you.
Your awakeFromInsert should look something like this:
- (void) awakeFromInsert
{
[super awakeFromInsert];
NSManagedObject* rect = [NSEntityDescription
insertNewObjectForEntityForName:@"Rect" inManagedObjectContext:[self
managedObjectContext]];
[self setValue:rect forKey:@"rect"]; //sets the value of the Shape-
>Rect relationship, inverse relationship gets set automatically
NSManagedObject* color = [NSEntityDescription
insertNewObjectForEntityForName:@"Color" inManagedObjectContext:[self
managedObjectContext]];
[self setValue:color forKey:@"color"]; //sets the value of the Shape-
>Color relationship, inverse relationship gets set automatically
}
Note that the keys are the property names for the Rect and Color
relationships in your Shape entity that are defined in your Core Data
model. (It's also possible to write the more attractive 'self.rect =
rect;' and 'self.color = color;' if you've declared the properties to
the compiler. This is described in the Core Data programming guide,
but adding the declarations might be a complication you don't need at
this stage. 'valueForKey:' and 'setValue:forKey:' are perfectly fine
for getting and setting properties.)
2. NSArrayControllers are not part of Core Data. Core Data doesn't
know anything about them. NSArrayControllers are Core-Data-aware,
though, if you set them to "entity" mode. In that case, the array
controller is capable of creating a Core Data object (when, for
example, you send it an 'add:' message), and it uses the specified
entity type and managed context to insert a new object into the object
graph. If you specified the Shape entity, it would create the new
shape, and your awakeFromInsert would automatically add the color and
rect to the new object.
If you have a second array controller bound to a set of shapes, it
will find out about the new object via the KVO notification that its
binding registered for, and update itself accordingly.
Note that the Core Data objects are not *in* the array controllers,
they're in the object graph (i.e. data model). Array controllers are
observers *of* the data model.
_______________________________________________
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