• 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
Re: Core data one-to-many: how to set relationship for newly added 'many' objects?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Core data one-to-many: how to set relationship for newly added 'many' objects?


  • Subject: Re: Core data one-to-many: how to set relationship for newly added 'many' objects?
  • From: "Sean McBride" <email@hidden>
  • Date: Wed, 22 Oct 2008 23:04:53 -0400

Jerry Krinock (email@hidden) on 2008-10-22 8:31 PM said:

>> A basic question:
>> I have a Core Data model which has a one-to-many relationship, say
>> one Department and several Employees.
>
>> When an Employee is added using the 'add' button of the standard
>> interface, how can I set the relationship to the (one) instance of
>> Department? It means you need access to theDepartment from Employee
>> - awakeFromInsert.
>
>
>I don't think so.  Getting a reference to one of your objects should
>be trivial.  Note that -[NSManagedObject
>insertNewObjectForEntityForName:inManagedObjectContext] returns the
>newly-inserted object.

True, but NSArrayController's add: method does not, which I think the OP
was referring to.

One option is to not use NSArrayController's add: and instead wire your
'add' button to your own action method.  In that method, you can call
insertNewObjectForEntityForName:inManagedObjectContext: and change the
resulting object's relationships.  A disadvantage of not using
NSArrayController's add: is that the controller's selection will not
change to select the newly added object (which is often desirable in
your UI).  So another option is to subclass NSArrayController and
override the newObject method.  There you can call super to create the
new object and then modify and return the object.  You can keep this
subclass more generic by making newObject call super, then use a
delegate to mutate the new object, then return it.

hth,

Sean

--
"Nothing will benefit human health and increase the chances for survival
of life on earth as much as the evolution to a vegetarian diet" - Albert
Einstein

_______________________________________________

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

References: 
 >Core data one-to-many: how to set relationship for newly added 'many' objects? (From: Arthur C. <email@hidden>)
 >Re: Core data one-to-many: how to set relationship for newly added 'many' objects? (From: Jerry Krinock <email@hidden>)

  • Prev by Date: problem with [NSImage drawAtPoint:fromRect:operation:fraction:]
  • Next by Date: Persistent Connection to Web Server in cocoa, emulating AJAX
  • Previous by thread: Re: Core data one-to-many: how to set relationship for newly added 'many' objects?
  • Next by thread: Localized pdf (and other) documents/files
  • Index(es):
    • Date
    • Thread