簡體   English   中英

(CoreData項目)為避免崩潰,我必須手動合成NSFetchedResultsController-為什么?

[英](CoreData Project) To avoid a crash I have to synthesize NSFetchedResultsController manually - Why?

當我不手動synthesize NSFetchedResultsController property ,我的應用程序崩潰。

崩潰日志:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070
2013-03-06 15:09:15.667 Staff Manager[6673:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (0) 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, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

當我按如下方式進行synthesize時,所有工作均按預期進行:

@synthesize fetchedResultsController = _fetchedResultsController;

你可以解釋嗎?

我的知識水平是Xcode的編譯器為我完成了這項工作。

Xcode版本:4.6部署目標:6.1

應用程序以這種方式崩潰(最后一行)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.tableView beginUpdates];
        Person *personToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];

        [self.managedObjectContext deleteObject:personToDelete];
        [self.managedObjectContext save:nil];

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self performFetch];

        [self.tableView endUpdates];
    }
}

我只能說的是,當您使用帶有已創建的nsfetchresultscontroller的xcode創建項目時,fetchresultscontrollerdelegate的創建方式是,當您刪除顯示的托管對象時,它已經執行deleteRowsAtIndexPaths,它會自動刪除相應的tableview單元格

您應該只保留deleteObject部分

Person *personToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];

[self.managedObjectContext deleteObject:personToDelete];
[self.managedObjectContext save:nil];

暫無
暫無

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

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