8

I am using pretty standard implementation of fetchedResultsController for output in tableView. In the end of -viewDidLoad I am making first call:

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error])
{
    NSLog(@"Error! %@",error);
    abort();
}

this is my fetchedResultsController:

 - (NSFetchedResultsController *) fetchedResultsController
 {   
     if (_fetchedResultsController != nil)
     {
         return _fetchedResultsController;
     }
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Preparation"
                                          inManagedObjectContext:_context];
     [fetchRequest setEntity:entity];

     int i = 1;
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ismer == %d", i];
     fetchRequest.predicate = predicate;

     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];

     fetchRequest.sortDescriptors = sortDescriptors; 

     _fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];
     _fetchedResultsController.delegate = self;
     NSLog(@"_fetchedResultsController.fetchedObjects.count - %d",            _fetchedResultsController.fetchedObjects.count);

     return _fetchedResultsController;
 }

my tableView methods:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [secInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    CDPreparation *drug = (CDPreparation *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", drug.name];
    return cell;
}

So, question is:

in log of _fetchedResultsController.fetchedObjects.count is equal to 0, but visually tableView is filled with objects. Why I have two different results for count?

Olex
  • 1,656
  • 3
  • 20
  • 38

1 Answers1

13

An NSFetchedResultsController doesn't actually perform the fetch request until you call performFetch:, so the result count is 0.

If you log fetchedObjects.count after calling performFetch:, you'll see a number which matches the tableView row count.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
  • @David Caunt can you please help me out with this http://stackoverflow.com/questions/19358095/core-data-reset – Ranjit Oct 14 '13 at 10:26