1
votes

Looking for some help with pointers and core data, i'll try and explain with a simple example.

Lets say I have a core data managed object called MOAdult which has 1 String attribute called 'name' and 1 NSDecimal Number attribute called 'age'.

I have another managed object called MOChild which also has 1 String attribute 'name', and 1 NSDecimal Number attribute called 'age'.

(I know I would normally subclass here but i'm not for this demo)

I have a 1 to many relationship between MOAdult and MOChild which creates a set which I'll call 'children'. The MOAdult also has a 1 to 1 relationship with itself which i'll called 'marriedTo'.

So...

//I Create 2 adults

   MOAdult *father = create object in core data.......
   father.name = @"Bob";
   father.age = 47; //obviously creating a NSDecimalNumber

   MOAdult *mother = create object in core data.......
   mother.name = @"Mary";
   mother.age = 42; //obviously creating a NSDecimalNumber

   //create the marriage relationship
   father.marriedTo = mother;

   //I create a child object

   MOChild *child = create object in core data.......
   child.name = @"Billy"
   child.age = 7; //obviously creating a NSDecimalNumber

   //add the child to the father
   [father addChildObject:child];

Now, when I add child to the father object, I want my application to recognise the adult is married by looking at that relationship (father.marriedTo) and if its not null, add the same child object to the mother objects Children Set.

So in effect, both the Father and Mother MOAdult object have the same MOChild object in their Children Set and any change to the Child object is reflected in both the mother and fathers set.

I'm assuming this is best done either through KVO on the set within the MOAdult object, or within the addChildObject method?

Still getting to grips with core data so any advice appreciated.

2
And if the parent divorce, who gets the children ? :-) - Martin R
Lol, good point. Was just trying to think of an easy way to explain it ;) - CpBulletSponge

2 Answers

0
votes

Here's an option

Create a ParentsMO object with a 1-1 relationship called husband and a 1-1 relationship called wife and a 1-many relationship to children.

then, you just need to set the children to a parent object.

Does the same thing with less messing about.

I know you've got a contrived example, but if you were to try and do this with KVO, then you'd have to create an NSManagedObject Subclass anyway.

0
votes

Your setup is contradictory. You state

I have a 1 to many relationship between MOAdult and MOChild

which makes it impossible to

add the same child object to the mother objects Children Set

The latter instruction is clearly impossible because it is a to-one relationship.

You need to create two relationship properties for the child, e.g. mother and father. You could automate that by overriding the appropriate setter in the child managed object subclass.

-(void)setFather:(Adult *)father {
   self.father = father;
   if (father.marriedTo != nil) {
      self.mother = father.marriedTo;
   }
}

Do the same for mother, and you could also override the adult methods for adding another child.

I suppose even Martin R will agree that a divorce will ultimately not change the father/mother-child relationship. More importantly, you will have to ask yourself the question if marriedTo is really the best criterion to determine the other parent...