I'm having difficulty creating tableView sections using a relationship.
I have two entities with a relationship List <----->> Item.
I want the List to be the sections and the Item to be the rows. I set the sectionNameKeyPath
with a key path @"itemList"
.
And here's what the rest of my fetchedResultsController looks like
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
// Fetch Request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Item"];
[fetchRequest setFetchBatchSize:20];
// Sort Descriptors
NSSortDescriptor *itemSort = [[NSSortDescriptor alloc] initWithKey:@"displayOrderItem" ascending:YES];
NSSortDescriptor *sectionSort = [[NSSortDescriptor alloc] initWithKey:@"displayOrderList" ascending:YES];
NSArray *sortDescriptors = @[sectionSort, itemSort];
[fetchRequest setSortDescriptors:sortDescriptors];
// Fetched Results Controller
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"itemList" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
return _fetchedResultsController;
}
The result is that the fetchedResultsController doesn't populate the tableView at all. When I try it without sections, with sectionNameKeyPath:nil
and just setSortDescriptor:itemSort
, it populates the tableView fine. Also, numberOfSectionsInTableView
and controller didChangeSection
is properly set up.
I'm not sure what I'm doing wrong. Can anybody help me with this?
Thanks
Change the section name key path to itemList.listName
as the FRC is expecting a string name for the section, not a managed object 'representing' that section.