views:

133

answers:

1

OK so I have two entities in my data model (let's say entityA and entityB), both of these entities have a to-many relationship to each other.

I have setup a NSFetchedResultsController to fetch a bunch of entityA. Now I'm trying to have the section names for the tableview be the title of entityB.

sectionNameKeyPath:@"entityB.title"

Now this causes a problem, where by the section name returned from that relationship appears to be ({title1}) or ({title1,title2...titleN}) obviously depending on how many different entityB's are involved. This doesn't look great in a tableview and doesn't group the objects as I would like.

What I would like is a section per entityB title with entityA appearing under each section, under multiple sections if necessary. I'm at a loss as how I am supposed to achieve this whether I need to update the predicate to get the entity to appear multiple times or whether I need to update the section and header functions to do some processing as the controller loops through the objects.

Any help is appreciated :)

Thanks

+2  A: 

You will get that because the call @"entityB.title" is going to return an array|set of titles. That is what you are seeing as it is translating into:

NSSet *titles = [myEntityA valueForKeyPath@"entityB.title"];

To fix this, you need to reverse your NSFetchedResultsController so that you are fetching EntityA objects through EntityB objects. Then your sectionKeyPath (and your primary sort) would be against just @"title".

Marcus S. Zarra
I don't know how you get NSFetchedResultsController to fetch EntityA objects through EntityB objects, but I just fetched EntityB objects and used the UITableViewDataSource methods to get EntityA from what the NSFetchedResultsController gave me. It ended up not being not as bad as I originally thought it would be.
hanleyp