user134611
user134611

Reputation: 776

NSFetchedResultsController and NSPredicate and a section name

I have core data model with two entities that have a many-to-many relationship with each other.

Eg,

Conference <<---->> Speaker

A conference can have many speakers, and One speaker can be at many conferences.

Im trying to use NSFetchedResultsController the main entity for fetchRequest is the Speaker.

While it does list all speakers alright, but Im trying to get the section as the "Conference" name and list all speakers within that section. I want it so that the name "John" as speaker would appear in two sections. Which implies that he's a speaker at two conferences.

This is the sectionNameKeyPath i'm using: @"conferences.Name"

NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"conferences.Name"  cacheName:@"fCache"] autorelease];

It kinda doesn't work, i get {TedEx} as the section title when i use [sectionInfo name].

The other problem is that If I One speaker appears in one section, (conference), it wont appear in the other section (even if that section(conference) has that speaker object).

This is potentially because, I get the sectionName as a set of combined Conferences. TedEx, SXSW as a single section.

Some how the goal should be to single out the sectionNameKeypath to fetch only ONE Conference and list all speakers within each. Perhaps i should set some kind of a predicate?

Im kinda in a bind here, is the approach a problem? I've read about Predicates but its kinda hard to grasp if your new to core data.

Upvotes: 0

Views: 327

Answers (1)

Mark Leonard
Mark Leonard

Reputation: 2096

This is a little different from the way you are thinking of doing it, and I'm not sure if there is a better way to accomplish it with a FRC & sectionNameKeyPath like you want to or not, but perhaps this will work:

  1. Instead of fetching Speakers, just do a normal NSFetchRequest to get an NSArray called conferences. You could have this sorted by conferenceName or date or whatever you need.
  2. Then for your table view set the number of sections to the [conferences count].
  3. For number of rows in each section get the Conference object for that section [conferences objectAtIndex:section], and return the speakers count of that Conference.
  4. To populate the cell data, just get the Speaker object by called: [[conferences objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

Upvotes: 0

Related Questions