iosobjective-ccore-datansfetchedresultscontrolleruimanageddocument

Fetched Results Controller returns Number of Sections = 0


I am using a NSFetchedResultsController inside a UITableViewController. Unfortunately, [self.fetchedResultsController.sections count] returns a value of 0 which is incorrect. It should return 1 section:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger numberOfSections = [self.fetchedResultsController.sections count];
    return numberOfSections;
}

This is the code I am using to instantiate the NSFetchedResultsController and load the results:

@interface SearchQueryViewController ()

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property Search *selectedSearch;
@end

@implementation SearchQueryViewController

- (void)viewWillAppear:(BOOL)animated
{
    if (!self.managedDocument) {
        [[MyDocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) {
            self.managedDocument = document;
            // Uncommenting the following code lines does not change the outcome:
            /*self.fetchedResultsController = nil;
             [self.tableView reloadData];*/
        }];
    }
    [super viewWillAppear:animated];
}
# pragma Fetched Results Controller

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

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Search"];

    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"query" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];

    self.fetchedResultsController = [[NSFetchedResultsController alloc]
                                     initWithFetchRequest:request managedObjectContext:self.managedDocument.managedObjectContext
                                     sectionNameKeyPath:nil cacheName:nil];
    return _fetchedResultsController;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger numberOfSections = [self.fetchedResultsController.sections count];
    return numberOfSections;
}
// THIS IS NEVER BEING CALLED BECAUSE NUMBER OF SECTIONS IS RETURNED AS ZERO:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

    NSInteger numberOfRows = [sectionInfo numberOfObjects];
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
    Search *search = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = search.query;
    return cell;
}

What I have tried:

My Question

Do you have any idea or suggestion why the section count returned by my code is zero?


Solution

  • You need to call performFetch in your fetched results controller method.