Lorenzo B
Lorenzo B

Reputation: 33428

NSFetchedResultsController: doc clarification needed

I need some clarifications from the Apple documentation

The fetch request must have at least one sort descriptor. If the controller generates sections, the first sort descriptor in the array is used to group the objects into sections; its key must either be the same as sectionNameKeyPath or the relative ordering using its key must match that using sectionNameKeyPath.

I'm using a NSFetchedResultsController with sections in the following way

[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:mainContext
                                      sectionNameKeyPath:@"paymentDate"
                                               cacheName:nil];

As I understood from the doc, the NSFetchRequest I pass in the NSFetchedResultsController must at least a sort descriptors. But since I use sections, the sort descriptor must be like the following:

[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:
        [NSSortDescriptor sortDescriptorWithKey:@"paymentDate" ascending:YES],
        [NSSortDescriptor sortDescriptorWithKey:@"paymentCode" ascending:YES],
        nil]];

If I use sort descriptors like

[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:
        [NSSortDescriptor sortDescriptorWithKey:@"paymentCode" ascending:YES],
        nil]];

I receive an error like the following

NSFetchedResultsController ERROR: The fetched object at index X has an out of order section name Y. Objects must be sorted by section name'

In my case, using [NSSortDescriptor sortDescriptorWithKey:@"paymentDate" ascending:YES], it allows me to overcome the problem. Maybe I'm only lucky ;).

But, what does it mean or the relative ordering using its key must match that using sectionNameKeyPath? Could you provide me an explanation?

Thank you in advance.

Upvotes: 0

Views: 146

Answers (1)

Jody Hagins
Jody Hagins

Reputation: 28349

If you are using sections, then the FRC needs to know how to sort the array of data so that it can group all the results by section. Thus, whatever you specify as the first sort descriptor in the fetch request, must match how your sections are to be grouped so that the FRC can do it properly.

You don't have to specify they exact field, but the sort order should be the same.

For example, if your first sort descriptor would group the array by section, even though it did not reference the section, then it would be fine. In most cases, you should explicitly specify your section attribute to remove any possibility for confusion on the part of the FRC -- or it will throw an exception.

EDIT

For example, let's say you have two sort descriptors: sd1 and sd2. sd1 uses the same key as provided to the FRC in sectionNameKeyPath. sd2 uses a different key.

When you sort the entire array, if they both produce a final array that groups the entities in the same sections, then you can use either sd1 or sd2 as the first sort descriptor.

Basically, they FRC will group the entities into sections based on the sectionNameKeyPath so it can determine how many sections there are, and how many rows are in each section. Thus, the array it gets as input must be sorted into similar groupings, relative to the sectionNameKeyPath.

As it trolls through the array, it looks at the sectionNameKeyPath and expects to see a specific number of entries with the same sectionNameKeyPath. Thus, the array must be sorted to keep all the sections together.

If you can accomplish that without specifying the same key as the first sort descriptor, then everything will work fine. If not, then you should use the same section key as the first sort descriptor.

Upvotes: 1

Related Questions