NSFetchedResultsController: sort descriptors and sections

I have a basic data model, for example ...

[Country] <--->> [League] <--->> [Match] 

And I use NSFetchedResultsController to display Matches in a UITableView .

I did this a million times before, but somehow the sections go wrong, and I can’t understand why.

I created sort descriptors, for example ...

 NSSortDescriptor *countrySD = [NSSortDescriptor sortDescriptorWithKey:@"league.country.name" ascending:YES]; NSSortDescriptor *leagueSD = [NSSortDescriptor sortDescriptorWithKey:@"league.name" ascending:YES]; NSSortDescriptor *dateSD = [NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:YES]; request.sortDescriptors = @[countrySD, leagueSD, dateSD]; 

First I want to check that I put them in the correct order. First you need to sort by country.name , then sort by league.name , and then sort by startDate .

i.e.

  • Everything in Albania should appear before anything in Spain .
  • In one country, anything in League 1 should appear before anything in League 2 .
  • In one league, all matches must be displayed in startDate order from the earliest first.

Then I create an NSFRC with this ...

 _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.moc sectionNameKeyPath:@"league.leagueID" cacheName:nil]; 

Thus, this should group the table by coincidence with different league.leagueID values.

It should be something like ...

 Albania - League 1 12:00 13:00 Albania - League 2 09:00 14:00 France - League 1 09:00 Spain - A League 08:00 12:00 Spain - B League 09:00 

This does not work. I get multiple headers for the same league. Some matches appear under the wrong heading, etc.

I checked the values ​​(NSLogged) of matches that are in the wrong league, and they really have the correct league. Therefore, even if they have Spain - A League , they appear under France - League A (for example).

Any idea how I can fix this?

+6
source share
1 answer

The key path used as the argument to sectionNameKeyPath must be the same key in the first sort descriptor (or generate the same relative ordering).

There is (as far as I know) a way to use two or more sort descriptors to group the results of the selected result controller into sections.

+15
source

All Articles