7

Sto utilizzando un'implementazione piuttosto standard di fetchedResultsController per l'output in tableView. Alla fine del -viewDidLoad sto facendo prima chiamata:fetchedResultsController.fetchedObjects.count = 0 ma è pieno di oggetti

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

questo è il mio 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; 
} 

miei tableView metodi:

- (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; 
} 

Quindi, la domanda è:

nel ceppo di _fetchedResultsController.fetchedObjects.count è uguale a 0, ma visivamente tableView è pieno di oggetti. Perché ho due risultati diversi per contare?

risposta

12

Un NSFetchedResultsController in realtà non eseguire la richiesta di recupero fino alla chiamata performFetch:, quindi il conteggio risultato è 0.

Se si accede dopo aver chiamato fetchedObjects.countperformFetch:, vedrete un numero che corrisponde al conteggio delle righe tableView .

+0

Grazie, hai ragione! – Alex

+0

@David Caunt puoi aiutarmi con questo http://stackoverflow.com/questions/19358095/core-data-reset – Ranjit

Problemi correlati