NSRangeException exception in NSFetchedResultsChangeUpdate event of NSFetchedResultsController

前端 未结 2 1328
傲寒
傲寒 2020-12-05 20:24

I have a UITableView that uses an NSFetchedResultsController as data source.

The core data store is updated in multiple background threads

2条回答
  •  庸人自扰
    2020-12-05 21:01

    I have now found a solution for my problem. In the case of an update event, there is no need to call

    [self.controller objectAtIndexPath:indexPath]
    

    because the updated object is already supplied as the anObject parameter to the -controller:didChangedObject:... delegate.

    I have therefore replaced -configureCell:atIndexPath: by a -configureCell:withObject: method that uses the updated object directly. This seems to work without problems.

    The code looks now like this:

    - (void)configureCell:(UITableViewCell *)cell withObject:(MyManagedObject *)myObj
    {
        cell.textLabel.text = myObj.name;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCellIdentifier"];
        [self configureCell:cell withObject:[self.controller objectAtIndexPath:indexPath]];
        return cell;
    }
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath
         forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
    {
        UITableView *tableView = self.tableView;
    
        switch(type) {
        /* ... */           
        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] withObject:anObject];
            break;
        /* ... */           
        }
    }
    

提交回复
热议问题