Re: Addressing Relationships in awakeFromInsert
Re: Addressing Relationships in awakeFromInsert
- Subject: Re: Addressing Relationships in awakeFromInsert
- From: Charilaos Skiadas <email@hidden>
- Date: Wed, 10 Aug 2005 18:16:06 -0500
On Aug 10, 2005, at 5:33 PM, August Trometer wrote:
Sorry for not being clear. I guess I'm not explaining this
properly, so I'll try again with more detail.
I've got 2 Entities: Show and Episode.
In my model, I've defined an inverse relationship between Show and
Episode, so that a Show has a to-many relationship (called
"episodes") with the Episode entity, and Epsiode has a to-one
relationship (called "show") with the Show entity. The relationship
from Show to Episode is optional, while the relationship from
Episode to Show is not (since you can't have an Episode without a
Show).
When I create a Show, everything is fine -- I can edit, update,
whatever. I'm not wanting Episodes to be created when a Show is
created, nor am I wanting a Show to be created when an Episode is.
In my UI, I'm using a contentSet to define what Episodes show show
in my list, based on the Show that is selected. This all works fine.
So, now I add an Episode for a particular show. Because everything
works up to this point -- episodes for a certain Show only appear
when that particular Show is selected -- I'm assuming that the
relationship does exist between Episode and Show. I want to take
advantage of that when the Episodes are created. That's where
awakeFromInsert comes in.
- (void) awakeFromInsert
{
[super awakeFromInsert];
[self setValue:[NSDate date] forKey:@"dateCreated"];
[self setValue:[self valueForKeyPath:@"show.createdBy"]
forKey:@"createdBy"];
}
All I want to do here is set default values in the newly created
Episode based on some of the data in the Show for which the Episode
was created. The user can then change this information as needed. I
would expect that I could traverse the keyPath to get the Show
information.
I'm sorry, how are you creating the new episode, and how are you
assigning a corresponding show for it? Just because some show is
selected somewhere in the UI, that doesn't mean that it will be
considered the "default show" for whatever episodes you create.
You are probably using a command like:
NSManagedObject *newEpisode = [NSEntityDescription
insertNewObjectForEntityForName:@"Episode"
inManagedObjectContext:context];
to add the episode (or maybe a controller is doing that for you
automatically). I expect awakeFromInsert is called at this exact
moment, before any relationships are set up, by you or a controller.
So of course you'll find Null as a value.
One more thing: If the episode's "createdBy" is always the same as
the corresponding show's "createdBy", then you don't need the
episode's createdBy. If it is the same, I don't see any reason why it
would be reasonable to initialize the value of the episode based on
the value of the show it is to be added in. It would make more sense
for the user to set some default values to be used when they work
with the application, or something like that.
Unless I've completely misunderstood what you are trying to do. I
haven't completely followed this thread from the beginning.
But when I try to reference the Show, I get a Null value, which
strikes me as odd, since the UI is obviously handling the
relationship between the two just fine.
This, I guess, leads me back to the beginning: what am I doing
wrong that the relationship between the two isn't showing up during
awakeFromInsert?
Thanks!
August
On Aug 10, 2005, at 4:02 PM, mmalcolm crawford wrote:
On Aug 10, 2005, at 1:31 PM, August Trometer wrote:
At no point am I setting the Master object -- this should be done
automatically. I'm assuming (maybe incorrectly?) that because of
the inverse Relationship set up between the Master and Detail
Entities that the "master" Relationship in my Detail object
automatically points back to the Master object that it's created
within.
"It is important to note that simply defining a relationship does
not cause a destination object to be created when a new source
object is created. In this respect, defining a relationship is
akin to declaring an instance variable in a standard Objective-C
class. Consider the following example.
@interface Widget : NSObject
{
Sprocket *_sprocket;
}
If you create an instance of Widget, an instance of Sprocket is
not created unless you write code to cause it to happen (for
example, by overriding the init method). Similarly, if you define
an Address entity, and a non-optional to-one relationship from
Employee to Address, then simply creating an instance of Employee
does not create a new Address instance. Likewise, if you define a
non-optional to-many relationship from Employee to Address with a
minimum count of 1, then simply creating an instance of Employee
does not create a new Address instance."
mmalc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40math.uchicago.edu
This email sent to email@hidden
Haris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden