• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Core Data: Instantiating linked entities
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Core Data: Instantiating linked entities


  • Subject: Core Data: Instantiating linked entities
  • From: Renaud CĂ©roce <email@hidden>
  • Date: Mon, 01 Sep 2008 12:15:24 +0200

Core Data: Instantiating linked entities


Hi everyone,

I have been searching the web for days now and I still have no answers to
my questions which I thought were very basic in the use of Core Data. Sorry
if this subject has already been addressed.

Here is my (simplified) model:

    Shape		 				   Color
--------------                                         -------------
Attributes:                                            Attributes:
  None                                                   red
--------------                                           green
Relationships:                                           blue
  color         <--------------------------+           --------------
  rect          <-------------+            |           Relationships:
                              |            +---------->  shape
                              |
                              |
                              |                            Rect
                              |                        --------------
                              |                        Attributes:
                              |                          height
                              |                          width
                              |                          x
                              |                          y
                              |                        --------------
                              |                        Relationships:
                              +----------------------->  shape



The Shape entity is implemented as a ShapeManagedObject class which
inherits NSManagedObject.
Color and Rect are both defined as NSManagedObjects.

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 ?


Please note I need to support 10.4.

Any answer or pointing to a doc or example code will be greatly
appreciated.
Thanks in advance !


Renaud Pradenc
--------------
ceroce.com



_______________________________________________

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

  • Follow-Ups:
    • Re: Core Data: Instantiating linked entities
      • From: Chris Hanson <email@hidden>
    • Re: Core Data: Instantiating linked entities
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Finder Eject Button in OutlineView
  • Next by Date: Re: how to delete the current user's directory?
  • Previous by thread: Re: Finder Eject Button in OutlineView
  • Next by thread: Re: Core Data: Instantiating linked entities
  • Index(es):
    • Date
    • Thread