Skip navigation.
 
mlre: Relationships during awakeFromInsert
FROM : Ben Trumbull
DATE : Sat Feb 02 09:11:04 2008

>In my Core Data Document based application, I need to initialize some
>attributes in instances one class (MOB) using attributes from another
>class (MOA, which is the root object of the document) to which the
>first is related.  Here is what the classes each look like:
>
>    MOA
>    _____
>    aTitle - string
>    seed - double
>    myMOBs - one-to-many relationship to MOB
>
>
>    MOB
>    _____
>    bTitle - string
>    seeded - double
>    myMOA - many-to-one relationship to MOA
>
>
>In MOB's awakeFromInsert I have the following:
>
>    - (void)awakeFromInsert
>    {
>        [super awakeFromInsert];
>
>        MOA* anMOA = self.myMOA;
>        self.seeded = anMOA.seed;
>    }
>
>What I'm seeing is that self.myMOA is 'nil' in this awakeFromInsert,
>so apparently the relationship has not yet been established.


MOB just got allocated.  The -initWithEntity... method is usually
still on the stack frame.  It doesn't have a .myMOA yet.  It's not
clear from your description why one would expect otherwise.

>I've tried intercepting setValue:forKey: and setPrimitiveValue:forKey: but
>they don't appear to be called during creation of an MOB.


self.seeded = foo;

is

[self setSeeded:foo];

not

[self setValue:foo forKey:@"seeded"];


>Where can I set MOB's 'seeded' using MOA's 'seed'?


Presumably you have some code somewhere that does something like:
NSManagedObject* MOB = [NSEntityDescription
insertNewObjectForEntityForName:@"MOB" inManagedObjectContext:moc];

Right after that would work.

If you have a reference to MOA during -awakeFromInsert (like in a
global, or through the appDelegate), and doing things the easy way
doesn't work, you could do something like -performSelector:...
afterDelay:0 or in response to the MOC's
NSManagedObjectContextObjectsDidChangeNotification.

--

-Ben

Related mailsAuthorDate
mlRelationships during awakeFromInsert Mike Rossetti Feb 2, 06:20
mlre: Relationships during awakeFromInsert Ben Trumbull Feb 2, 09:11
mlRe: Relationships during awakeFromInsert Mike Rossetti Feb 2, 23:59