Re: Core data one-to-many: how to set relationship for newly added 'many' objects?
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