Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with many to many relationships with NSFetchedResultsController?

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

like image 447
Phil Yates Avatar asked Apr 19 '10 21:04

Phil Yates


1 Answers

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".

like image 108
Marcus S. Zarra Avatar answered Sep 25 '22 18:09

Marcus S. Zarra