Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

iphone - Where can I find a good example of a Core Data to-many relationship?

Does anyone have a tutorial or source code that shows a to-many relationship being used, where the user add elements in on the fly? I would just like to have a look because reading about it hasnt been much help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

People are often confused by to-many relationships because one entity represents the relationship as a set while the other represents it as a single object. Take the following entities:

EntityA{
    name:string
    bees<-->>EntityB.a
}

EntityB{
    name:string
    a<<-->EntityA.bees
}

In EntityA, the relationship bees is a set because there maybe many EntityB objects in the relationship. So, using Key-Value coding, you would have to access the relationship using a mutableSetForKey: expanding everything out to see the detail would like so:

NSMutableSet *muteSet=[anEntityAObj mutableSetValueForKey:@"bees"];
[muteSet addObject:aNewBObj];
[anEntityAObj setValueForKey:@"bees"];

...or more compactly:

[[anEntityAObj mutableSetValueForKey:@"bees"] addObject:aNewBObj];

If you set from the EntityB side, however, you are only adding a single object so you can just use setValueForKey: directly like so:

[anEntityBObj setValueForKey:anEntityAObj];

That's if you use generic NSManagedObject instances to represent your entities. If you create custom subclasses then you have properties and methods to do the setting for you:

[anEntityAObj addBeesObject:anEntityBObj];
anEntityBObj.a=anEntityAObj;

Remember as well that with managed objects, setting one side of a relationship defined as reciprocal automatically set the other side as well and removing works the same way.

Update

Lets say i've got 2 entities -- Person:with "name" attribute -- Times:with "time" attribute -- i would want to set multiple times for each name, but how to I tell it which name i would like to add the specific times to?

You don't create relationships with attributes, in this case name but rather with an object, in this case an instances of the Person entity/class. Each individual Person object is completely separate from all other Person objects even if they have the same value in their name attribute.

You must obtain a reference to any particular Parent object. If you have just inserted a new Parent object then you already have a reference to it. If it is already been inserted/persisted, then you create a fetch with a predicate that will return the proper object. Once you have the correct Parent object you then just add the Time objects to the relationship.

So, if your entity looks like this pseudo-code:

Parent{
    name:string
    times<-->>Time.parent
}

Time{
    theTime:date
    parent<<-->Parent.times
}

... and you are using generic NSManagedObjects to instatiate you entities, you set the relationship between an existing Parent object and new Time object something like this:

NSManagedObject *existingParent= //... results of a fetch
    NSManagedObject *newTime=[NSEntityDescription insertNewObjectForEntityForName:@"Time" inManagedObjectContext:self.moc];
    [newTime setValue:[NSDate date] forKey:@"theTime"];
    [newTime setValue:existingParent forKey:@"parent"];

Note that if you set the relationship from the Time object's side, you can use setValue:ForKey: because from the Time object's perspective the relationship is just one object to one object.

It is really quite simple once you start thinking in objects instead of databases. Each object you insert in a context is unique even if it shares attributes with other objects of the same entity/class. That is why you can set a relationship between specific objects without necessarily worrying about the values stored in their attributes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...