簡體   English   中英

無法從fetchedResultsController刪除最后一個對象

[英]Can't delete last object from fetchedResultsController

我使用NSFetchedResultsController填充tableview,並且一切正常(添加,刪除..)。但是每當我嘗試從tableview中刪除最后一行時(當tablview中只有一行時),應用程序崩潰並顯示以下日志:

 *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037
2012-08-03 16:32:39.667 MackaWithCoreData[793:15503] CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Invalid update: invalid number of rows in section 0.  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 (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

據我所知,在刪除對象后,由於fetchedResultsController為null,委托方法嘗試重新加載tableview時,它將導致崩潰。如何處理此問題? 提前致謝

編輯 controller controllerDidChangeContent:.. 方法如下

 - (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];
            XLog(@"the row removed from tv");

            break;

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

        case NSFetchedResultsChangeMove:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            XLog(@" object deleted from fetchedresultscontroller");

            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

幾天后,我決定采用另一種邏輯來解決此問題。 我首先控制fetchedObjectsfetchedResultsController的計數,如果計數大於1,則調用數據庫上的刪除和保存委托,但是如果計數等於1,則只需推送到其他viewController。 同樣在該vc上,我將禁用導航控制器回到該視圖控制器,直到出現新消息。 當收到新消息時,我將首先刪除實體上的對象,然后添加消息。

這是一些偽代碼和算法:

     if ([[self.fetchedResultsController fetchedObjects]count] >1) {
         callDeletionDelegateForDatabase;
      }else{
       declare a viewController object;
       pushToThatVC;
      }

//在目標vc的類上

hide_back_navigation_back_to_theViewControllerYouCameFrom;
     //when a new message comes from web service
     reset Entitiy(the property we need its value)
     show_navigation_Controller_To_theViewControllerOfMessages...

我發布了這個愚蠢的偽代碼,以幫助遇到相同情況的其他人,如果有人知道更好的方法,請寫下來。 下面是我的代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
         if ([[self.fetchedResultsController fetchedObjects]count] >1) {

        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. 
             */ 
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
            abort(); 
            } 
         }else {
             BNT_DetailViewController *vc=[BNT_DetailViewController new];
             [self.navigationController pushViewController:vc animated:YES];

             XLog(@"You Cant delete this");
         }
     } 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM