7

Tôi đang sử dụng việc triển khai khá chuẩn của fetchedResultsController cho đầu ra ở tableView. Trong phần cuối của -viewDidLoad Tôi đang thực hiện cuộc gọi đầu tiên:fetchedResultsController.fetchedObjects.count = 0 nhưng nó đầy các đối tượng

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

đây là fetchedResultsController tôi:

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

tableView phương pháp của tôi:

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

Vì vậy, câu hỏi là:

trong nhật ký của _fetchedResultsController.fetchedObjects.count bằng 0, nhưng trực quan tableView được lấp đầy với các đối tượng. Tại sao tôi có hai kết quả khác nhau để đếm?

Trả lời

12

Một NSFetchedResultsController không thực sự thực hiện các yêu cầu lấy cho đến khi bạn gọi performFetch:, vì vậy số lượng kết quả là 0.

Nếu bạn đăng nhập fetchedObjects.count sau khi gọi performFetch:, bạn sẽ thấy một số trong đó phù hợp với số lượng hàng tableView .

+0

Cảm ơn, bạn đã đúng! – Alex

+0

@David Caunt bạn có thể vui lòng giúp tôi với http://stackoverflow.com/questions/19358095/core-data-reset này – Ranjit

Các vấn đề liên quan