NSFetchedResultsController has 0 sections

I have a problem, I don’t know where it came from, related to CoreData. My database has a set of categories (with a name and description) that contain elements (using a one-to-many relationship).

I want to split my table view in sections with a class attribute Category, but when I try to do this using sectionNameKeyPath:, the result NSFetchedResultsControllerwill be 0 sections. If I pass nil to this parameter, it has 1 section.

The code is as follows:

- (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;
    }
}
+5
source share
1 answer

. NSFetchedResultsController. , sectionNameKeyPath ( "" ) , ( "checked" ). , .

, "" .

+1

All Articles