Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController has 0 sections

I'm having a problem I don't know where it comes from, related to CoreData. In my database, a have a set of categories (with name and description), which contain elements (using a one-to-many relationship).

I want to divide my table view in sections given an attribute of the Category class, but when I try to do it using sectionNameKeyPath:, the resulting NSFetchedResultsController has 0 sections. If I pass nil to this parameter, it has 1 section.

The code is the following:

- (NSFetchedResultsController*) fetchedResultsController
{
    if(fetchedResultsController)
        return fetchedResultsController;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:10];

    // Edit the sort key as appropriate.

    NSSortDescriptor *checkDescriptor = [[NSSortDescriptor alloc] initWithKey:@"checked"
                                                                   ascending:YES];
    NSSortDescriptor *indexDescriptor = [[NSSortDescriptor alloc] initWithKey:@"orderIndex"
                                                                   ascending:YES];
    NSArray *sortDescriptors = @[checkDescriptor, indexDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                   managedObjectContext:self.managedObjectContext
                                                                     sectionNameKeyPath:@"checked"
                                                                              cacheName:nil];

    NSError *error = nil;
    if (![fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
        return nil;
    } else {
        fetchedResultsController.delegate = self;
        return fetchedResultsController;
    }
}
like image 959
gskbyte Avatar asked Nov 13 '22 23:11

gskbyte


1 Answers

See the NSFetchedResultsController documentation: The key used for sectionNameKeyPath ("name" in your case) must be the same key used in the first sort descriptor ("checked" in your case). They can be different, but then both keys must generate the same relative ordering.

In your case I assume that you want to add an additional sort descriptor on "name" and use that as the first sort descriptor.

like image 103
Martin R Avatar answered Dec 05 '22 08:12

Martin R