开发者

How do I Retrieve Relative Scores From a Subset of Entities in CoreData

Sorry for the long question but I think it needs some background :S

I have the following model in CoreData:

Model http://mikeholman.org/model.png

A Guest can have any number of Scores in its scores relationship. A Score can have a maximum of two 开发者_运维问答Guests in its guests relationship.

For each pair of Guest entities I model have a Score entity with a value indicating how well they get on with one-another.

In my interface, I want users to be able to select any Guest from a list, then view a second list of all the other Guests and the score that associates the each of those Guests with the originally selected Guest like so:

alt text http://mikeholman.org/interface.png

Fetching an array of all Guests for the first table (tv1) is simple enough. Currently for the second table I am fetching all the other Guests by filtering this array using the following predicate:

[NSPredicate predicateWithFormat:
    [NSString stringWithFormat:@"not displayName == '%@'", 
        [[allGuests objectAtIndex:[tv1 selectedRow]] valueForKey:@"displayName"]]];

Which works OK but my problem is knowing how to display the correct Score.value. I feel that I should instead be using the relationship of the selected Guest.scores to populate the second tableview but I'm not sure how to do this. Any pointers would be welcome - thanks!


I've managed to figure this out for myself at last. This is how I ended up doing it:

  1. Keep a reference to the selected Guest entity from the first tableview.
  2. Use the request all Score objects from the ManagedObjectContext filtered with an NSPredicate:

    @"ANY guests.displayName=%@", [guestA displayName]

  3. Use the returned Score objects to retrieve the value

  4. Retrieve the guests for each individual Score object from (3), filtered with another NSPredicate:

    @"NOT displayName=%@",[guestA displayName]

  5. The resulting filtered NSMutableSet must contain the Guest associated with the first selected Guest via the current Score as the Score.guests relationship has a maximum of 2 Guest objects. Therefore excluding the known selected Guest gives the required answer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜