in Apps, Cocoa, Core Data, Objective-C, Programming

Core Data Objects in Wrong Sections

NSFetchedResultsController is a really handy class. Use one of the default Core Data templates in Xcode and you’ll very quickly have a nice list of managed objects in a table view. With a few more lines of code you can get the NSFetchedResultsController to group your objects by sections. You do this by specifying a key-path in the class’s constructor method but there is another step that if overlooked will cause some confusion.

In a sample app I’ve created a food table that lists food in categories.

FetchedResultsController method grouping sections using a key-path:

Screenshot of Food sample app in wrong order.Save and quit the app a few times and you’ll see the objects seem to be in the wrong sections. If you look closer you’ll see that the objects are actually sorted in ascending name order. On looking at the code, it seems this is exactly what we asked the program to do! After some testing it also seems to show up more often if the table is a grouped one.

As per the docs, after you specify a key-path to group each section with you also need to make sure the first sort descriptor is sorting this key-path. Add a sort descriptor and everything will work as expected.

Revised fetchedResultsController method with missing sort descriptor:

Write a Comment



  1. Where are the section headers “Fruit” and “Vegetable” coming from?

    How do you set up the relationship between a manually created section header and a core data entity?


    • Hi AD,

      Fruit and Vegetables are values of the type property you’d create on the entity.

      So with the RWFruit entity that I’ve got in my example, it has a type property and a name property. All the fruits have the type set to “Fruit” and all the vegetables have the the type set to “Vegetable”. Then you set the sectionKeyPath to “type” and it will group all the objects that have matching types.

      Hope that helps explain it, I might upload an example project if it helps.

    • I’m glad I could help Denis. I’m trying to jot down as many of these little gotchas as I encounter them.

  2. How do you not show the “quick search index sidebar” on the right of the screen? When ever is set “sectionNameKeyPath” i get the first letter capitalized with a scroll bar. Noticed yours doesn’t have this.

    Thanks in advanced!

    • Hi Pyraego, you must have implemented – (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView in your UITableViewDataSource?

      • AHA yes! Just searched that method and found it in “CoreDataTableViewController”. Now I just need to figure out how to remove this. This file is a template I found when going through core-data tutorials.

      • Got it working!! Ended up returning nil for that method. Thanks for all the help. You can delete the previous post if you want. Sorry for posting it “anonymous”

  3. Thanks, this was EXTREMELY useful.

    > If this **sectionNameKeyPath** is not the same as that specified by the **first sort descriptor** in fetchRequest, they **must** generate the **same relative orderings**. For example, the first sort descriptor in fetchRequest might specify the key for a persistent property; sectionNameKeyPath might specify a key for a transient property derived from the persistent property.

  4. Thanks so much. I was struggling with this section info keypath issue for more than a week.
    Its kind of illogical by Apple that even after mentioning group by section names, it is grouping objects in wrong sections. Adding an extra sort key fixed for me.


  • Core Data Objects in Wrong Sections | sayem khan / blog January 15, 2013

    Thanx so much