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.


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:

  • 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.
  • Then for your table view set the number of sections to the [conferences count] .
  • 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.
  • To populate the cell data, just get the Speaker object by called: [[conferences objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  • 链接地址: http://www.djcxy.com/p/36166.html

    上一篇: NSFetchedResultsController按名字或姓氏排序

    下一篇: NSFetchedResultsController和NSPredicate以及部分名称