Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting NSFetchedResultsController, NSSortDescription and sectionNameForKeyPath to work together
    text
    copied!<p>I'm currently working on an App that has a couple of Entities and relationships as illustrated below:</p> <p><strong>Item</strong> &lt;&lt;--> <strong>Category</strong>.</p> <p>I am currently fetching <strong>Item</strong> instances and displaying them in sections using the item's <code>category.name</code>. In this case I can use a sort descriptor to sort the categories by name, which is pretty straightforward and working fine (relevant code below):</p> <pre><code>-(NSFetchedResultsController*)fetchedResultsController { if (fetchedResultsController_ != nil) return fetchedResultsController_; NSManagedObjectContext *moc = [order_ managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:moc]; [fetchRequest setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category.name" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; [sortDescriptors release]; [sortDescriptor release]; NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:moc sectionNameKeyPath:@"category.name" cacheName:nil]; controller.delegate = self; self.fetchedResultsController = controller; [controller release]; [fetchRequest release]; NSError *error = nil; if (![fetchedResultsController_ performFetch:&amp;error]) { // Error handling } return fetchedResultsController_; } </code></pre> <p>My problem now is that I need to sort these categories not by name, but by a <code>(NSNumber*) displayOrder</code> attribute that is part of the <strong>Category</strong> entity. <strong>BUT</strong> I need the section titles on the tableview to continue to use the categorie's <code>name</code>.</p> <p>If I set the sortDescriptor to use <code>category.displayOrder</code> and keep the sectionNameKeyPath as <code>category.name</code>, the section titles work fine but the sortDescriptor is simply ignored by the fetchedResultsController and the table sections are ordered by the category's name (not sure why??).</p> <p>My next idea was to overwrite the <code>displayOrder</code> getter method but that didn't get me too far as the return types are different, plus I needed the actual displayOrder value for the section sorting.</p> <p>So right now I have a solution which feels a bit clunky (code below), and I am wondering if there is a better way of achieving the same thing using the fetchedResultsController alone.</p> <pre><code>- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section // The code below grabs a reference to first object for a given section // and uses it to return the associated category name { id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; NSArray *menuItems = [sectionInfo objects]; if ([menuItems count] &gt; 0) { MenuItem *menuItem = [menuItems objectAtIndex:0]; NSString *categoryName = menuItem.category.name; return categoryName; } return [sectionInfo name]; } </code></pre> <p>Am I missing something basic here?</p> <p>Thanks in advance for your thoughts.</p> <p>Rog</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload