0

我有一個父類,處理所有的UITableview持久性數據管理和UITableView行managemetn。我已經將一個新的XCode4項目的大部分代碼複製到UITableview的持久數據中。現在我試圖刪除子類中的一個表格,並且在刪除一行時沒有任何委託方法被調用,導致我的tableview處於不一致的狀態。iPhone4 iOS5 NSFetchedResultsController如何在一個子類中正確設置委託?

我是否正確地指派委託?在這種情況下誰是NSFetechedResultsController的委託人? 我是否需要明確地讓我的子類成爲NSFetchedResultsControllerDelegate?

下面是我如何檢索NSFetchedResultsController,請注意它如何將自我指派爲委託。

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (__fetchedResultsController != nil) 
    { 
     return __fetchedResultsController; 
    } 

    /* 
    Set up the fetched results controller. 
    */ 
    // Create the fetch request for the entity. 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    // Edit the entity name as appropriate. 
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext]; 

    [fetchRequest setEntity:entity]; 

    // Set the batch size to a suitable number. 
    [fetchRequest setFetchBatchSize:fetchBatchSize]; 

    // Edit the sort key as appropriate. 
// NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startMinute" ascending:YES]; 
    NSArray *sortDescriptors; 
    if(sortDescriptor2!=nil) 
    { 
    sortDescriptors= [[NSArray alloc] initWithObjects:sortDescriptor,sortDescriptor2, nil]; 
    }else 
    { 
    sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    } 
    [fetchRequest setSortDescriptors:sortDescriptors]; 

    if(filterPredicate != nil) 
    { 
     [fetchRequest setPredicate:filterPredicate]; 
    } 

    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"]; 
    aFetchedResultsController.delegate = self; 
    self.fetchedResultsController = aFetchedResultsController; 



    NSError *error = nil; 
    if (![self.fetchedResultsController performFetch:&error]) 
     { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
//  abort(); 
    } 

    return __fetchedResultsController; 
} 

當它的時間來刪除表中的行,沒有的委託方法被調用,甚至想到了錶行是真正刪除。爲了調試問題,我添加了以下2個斷言。第二個斷言失敗。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    id delegate = self.fetchedResultsController.delegate; 


    NSAssert(delegate!=nil,@"Tableview is not delegate of fetched results controller!"); 

//this assertion fails 
    NSAssert([((UITableViewController*)self) isEqual:delegate],@"Tableview is not delegate of fetched results controller!"); 

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 


     // Delete the managed object for the given index path 
     NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; 

     [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; 

     // Save the context. 
     NSError *error = nil; 
     if (![context save:&error]) 
     { 
      /* 
      Replace this implementation with code to handle the error appropriately. 

      abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
      */ 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
//   abort(); 
     } 
    } 
} 

謝謝!

回答

1

設置NSFetchedResultsController的代碼看起來是正確的。它的代表設置爲self意味着代表是您創建它的對象。這看起來像tableView的代表相同的對象。它也看起來像你正確地刪除對象並保存上下文。

你沒有顯示,可能是崩潰的原因是你處理NSFetchedResultsController委託方法。至少,你需要添加以下內容:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView reloadData]; 
} 

這將迫使表重新加載時,它檢測到其managedObjectContext變化的時候,fetchedResultsController自我更新。

如果這不是你的問題,那麼你需要發佈一些更多的代碼來展示你如何配置類以及如何處理fetchedResultsController委託方法。

編輯:固定不正確的簽名OM controllerDidChangeContent:

1

你必須使用這些方法的NSFetchedResultsController能夠在您的表視圖的變化作出反應:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller; 
{ 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{ 
    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      // your code for insert 
     break; 
     case NSFetchedResultsChangeDelete: 
      // your code for deletion 
     break; 
    } 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 
    { 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
     break; 

     case NSFetchedResultsChangeMove: 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    [self.tableView endUpdates]; 
} 

另外,還要使用NSFetchedResultsController的視圖控制器聽聽 NSFetchedResultsControllerDelegate