views:

184

answers:

1

I'm trying to make a Core Data app in which when you select one "Player" in a TableView, and a list of all teammates appears in a second tableView, with a column for how many times those two players have played on the same "Team" (another entity).

This has got me completely stuck, because while I know how to fill up a table from a normal array, using ArrayControllers and Core Data has really cluttered up my view of the situation.

How would you approach this?

+1  A: 

Yours is a Bindings problem, not a Core Data problem. :-)

You should definitely get a handle on Cocoa Bindings before dealing with Core Data. This is stated in the docs and is very true.

The subject of your question seems to differ from the body, so I'll answer both.

Showing the Teammates

Core Data aside, assume you have a table representing Player instances. Player has one Team. Team has many players. Therefore, it's inferred that an instance of Player has "team.players" (minus itself) as teammates. Whether you're using Core Data to manage the model or not, this is true of the overall relationships.

If you read through and master Cocoa Bindings, you'll find that this is not hard at all to set up using a basic Master/Detail setup (with an extra array controller for the Detail part, for simplicity). Your Master array controller represents all Player instances, while your detail array controller represents the Teammates - or the Master's selection's "team.players" (minus itself).

The Teammates array controller will have its entity and managed object context set up as usual (see the docs). The "contentSet" will be bound to the Master array controller's "selection" controller key, with "team.players" as the model key path.

The trick is to filter out the Master controller's selected player using predicates. This you can do with the array controller's Filter Predicate. Maybe one with a format of "self != %@", where "%@" represents the Master array controller's selection. I'll leave Predicates (a complicated topic unto itself) to you. Remember, you can set them in code ([myController setFilterPredicate:myPredicate]) or by using bindings. Predicates are independent of Core Data as well.

Getting Selection

Since the array controller is in charge of the array the table represents, it's best to ask the array controller what its selection is. One way is to ask its -arrangedObjets for the objects at its -selectedIndexes.

NSArray * selectedObjects = [[myArrayController arrangedObjects] objectsAtIndexes:[myArrayController selectedIndexes]];

You can also ask it for its -selectedObjects. There are differences between these two approaches that are described by the documentation (API reference and conceptual docs) that you should definitely understand, but asking the controller is the most important concept, regardless of whether you use an NSArrayController or some custom controller that conforms to the and protocols.

Disclaimer: Typed hastily after a social Sake evening. Not checked for errors. :-)

Joshua Nozzi
Thanks a lot, I realize I've asked a lot here, and you've given me plenty to follow up on. Again, cheers!
DanF
In case you're willing to further expand: These players are not always on the same team, and often play against each other. I hoped to have a log of "matches" with "team" entities for each. Since player-to-team is actually many-to-many, this requires some extra logic. Is this still a project for a sort of predicate, or will I need to subclass the Managed Objects? If I subclass and create my own method to return a playCount, will I be able to use IB bindings to access it?
DanF