开发者

Relationship Modelling in Core Data

I'm fairly new to Obje开发者_如何学编程ctive C and Core Data and have a problem designing a case where players team up one-on-one and have multiple matches that end up with a specific result.

With MySQL, I would have a Player table (player primary key, name) and a match table (player A foreign key, player B foreign key, result).

Now how do I do this with Core Data? I can easily tie a player entity to a match entity using a relationship. But how do I model the inverse direction for the second player ref. in the match entity?

Player

Name: Attribute

Match: Relationship Match

Match

Result: Attribute PlayerA: Relationship to Player (<- Inverse to Player.Match)

PlayerB: Relationship to Player (<- Inverse to ????)

Would be great if someone could give me an idea on this!

Thanks, Stevie.


If your match has a specific home player and away player, then you would use:

player.homeMatches <---->> match.homePlayer
player.awayMatches <---->> match.awayPlayer

You may still have the:

player.matches

if you need to search by all matches and you don't want to AND your search fields together. Or you can provide a method/readonly-property "allMatches":

- (NSSet *)allMatches
{
    return [[NSMutableSet setWithSet:self.homeMatches] unionSet:self.awayMatches];
}

to give the value at runtime. Of course, you can't search by this "allMatches" property using a fetch predicate but you can sort and access at runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜