Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

ios - NSFetchedResultsController: delete 1 row crashes the app

Deleting a row from a UITableView fed by an NSFetchedResultsController causes my app to crash.

Error is:

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:1330
*
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 (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).'

I want only swipe-to-delete. My deletion code goes like this:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{  
    [self.tableView beginUpdates];  
    SequenceData *sd = [self.fetchedResultsController objectAtIndexPath:indexPath];  
    [self.managedObjectContext deleteObject:sd];  
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];  
    [self.tableView endUpdates];  
}

My NSFetchedResultsController is set up just like in Ray Wanderlich's tutorial (http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller)

Number of rows is determined by:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    
    id sectionObjects = [[self.fetchedResultsController sections] objectAtIndex:section];  
    NSInteger nbObjects = [sectionObjects numberOfObjects];  
    return nbObjects;  
}

It looks like the fetch is not updating (number of rows does not vary). Do I need to fetch myself (isn't this taking care of by the fetch controller)?

There is obviously something basic I am missing here. Don't hesitate to suggest basic answers.

I first implemented this with the controller: didChangeObject: ... method. Error was the same, but some details differed.

Edit I believe my problem is fixed. Both answers (From CX and Martin) helped me find it. Martin got the answer because of the explanations that helped me understand a little bit better...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't call

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

if you use a fetched results controller. Only delete the object with

[self.managedObjectContext deleteObject:sd];

The fetched results controller delegate method didChangeObject: is then called automatically, and that calls deleteRowsAtIndexPaths:.

So in your case, the row was deleted twice, and that caused the exception.

Note that you don't need beginUpdates/endUpdates here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...