2011-05-20 56 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

相關問題