2011-05-20 40 views
0

我有一个奇怪的问题,需要一些帮助。fetchedResultsContext并没有真正删除对象,并导致commitEditingStyle中的断言问题

我在一个核心数据项目上,并没有使用fetchedResultsController,只是使用fetchRequets和数组来填充zableviews。所以现在我决定改变和使用FRC ...

到目前为止一切都非常简单...但是与commitEditingStyle我从那时起就有问题 - 当删除行时,我引发了一个像这样的异常:

The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update. 

在我发现那是因为我想删除遗体在FRC ...对象我把一些NSLog的章节中的Liko这样结尾:

NSLog(@"Number before deleting: %i - deleting %@",[[fetchedResultsController fetchedObjects] count], [fetchedResultsController objectAtIndexPath:indexPath]); 

    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; 
    [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

    NSLog(@"Number before saving: %i",[[fetchedResultsController fetchedObjects] count]); 

    NSError *error; 
    if (![context save:&error]) { 

     [NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]]; 
    } 

    NSLog(@"Number after saving: %i",[[fetchedResultsController fetchedObjects] count]); 

    NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath]; 
    [tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade]; 

,结果是这样的:

2011-05-20 14:49:35.398 Nivellator[6000:207] Number before deleting: 3 
2011-05-20 14:49:35.399 Nivellator[6000:207] Number before saving: 3 
2011-05-20 14:49:35.404 Nivellator[6000:207] Number after saving: 3 

当然,当我告诉tableview它会得到3行,但只能通过两个...当然会呕吐,但是这里有什么问题?

我的旧代码是这样看,没有任何问题,工作...

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([tableView isEqual:self.entityTableView] == YES) { 
     if (editingStyle != UITableViewCellEditingStyleDelete) { 
      return; 
     } 

     if ([self.entityArray count] <= indexPath.row) { 
      return; 
     } 

     Member *thisEntity = [self.entityArray objectAtIndex:indexPath.row]; 
     [delegate.managedObjectContext deleteObject:thisEntity]; 

     NSError *savingError = nil; 

     if ([delegate.managedObjectContext save:&savingError] == YES) { 

      // Remove the entity from the Array and delete the corresponding table cell with animation 
      // 
      [self.entityArray removeObject:thisEntity]; 

      NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath]; 
      [tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade]; 
     } else { 
      /* Error handling missing */ 
     } 
    } 
} 

OK

到底

它来到了RTFM ...所以我做了部分改变工作代码如:

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([tableView isEqual:self.entityTableView] == YES) { 
     if (editingStyle != UITableViewCellEditingStyleDelete) { 
      return; 
     } 

     NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; 
     [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

     NSError *error; 
     if (![context save:&error]) { 

      [NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]]; 
     } 

     if ([fetchedResultsController performFetch:&error]) { 

      [tableView beginUpdates]; 

      NSArray * cellsToDelete = [NSArray arrayWithObject:indexPath]; 
      [tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationFade]; 

      [tableView endUpdates]; 
     } else { 

      [NSException raise:NSGenericException format:@"Following error occured when trying to delete %@: %@", [fetchedResultsController objectAtIndexPath:indexPath], [error description]]; 
     } 
    } 
} 

但是,当我使用sectments在我的FRC我仍然得到同样的错误......我找不到任何比这更多的是与FRC ...

有什么想法?

+0

我不知道这是否仍然打开,但它看起来像由于deleteRowsAtIndexPath调用而出现错误。当你使用fetchedResultsController时,它会自行管理(假设你使用委托方法)。调用''[context save:&error]'通过FRC委托方法更新您的tableview,因此告诉表视图以后删除行会给您的代码带来不好的数据,并引发您所看到的异议。如果你仍然有这个问题,我希望这有助于澄清 – justin 2012-01-04 20:25:06

回答

0
  1. 删除
  2. 检查您是否已经正确实施FRC的委托方法后删除了FRC的缓存。
  3. 确保在运行FRC的委托方法之前冻结表格视图beginUpdates
+0

谢谢。这部分有助于......但我仍然有问题......请参阅重新编辑。 – rummsmummel 2011-05-21 05:10:04