开发者

Core Data modeling of relationships

I'm modeling my app in Core Data. I have an object called 'stats' that will have a 'player', a 'team', and a 'game' associated with each object. Once these stats objects are stored, I imagine querying for them by any one or more of these attributes (player, team, game). See model below.

Core Data modeling of relationships

Question 1: Does it make sense to model these as 'relationships' as opposed to 'attributes'.

Question 2: When retrieving I would like to form my NSPredicates using objects and not have to keep track of unique IDs names for each player, team, or game. If I store the player, team, and game objects (as opposed to unique IDs or names) with each entity, what is the best way to retrieve them. Would the following work if I wanted to retrieve all stats objects for a particular 'team' (someTeamObject) or do I need to query by ObjectID?

开发者_运维百科
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"team == %@", someTeamObject];

Thanks


  1. If I understand your question right, yes, it makes sense to model these as separate objects with relationships between them. It will especially help when fetching various objects; you could, for example, get the single Player associated with a given Stat, or get all the Stats for a Game. This leads into your second question:

  2. Instead of using a predicate, note that your relationships have names; instead of building a full NSFetchRequest with associated NSPredicate, you could just call the relevant accessor method. For your request to get all the Stat objects for a Team, you just call [team stats] and you get back an NSArray of Stat objects.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜