Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView - Table view in core data app crashing because number of rows not valid after deletion
    text
    copied!<p>I am creating a core data example wherein I want to delete managed objects (here called projects). Deleting the manages objects seems to work, but refreshing the tableView is a problem. Below is the log message I get when the app crashes when <code>tableView: numberOfRowsInSection:</code> is called.</p> <blockquote> <p>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 (13) must be equal to the number of rows contained in that section before the update (13), 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).'</p> </blockquote> <p>Here the number of rows before the deletion is 14 and after is 13. See the methods I am using for A. fetching the number of rows, which should equal the number of "projects" in core data. B. Deleting the project. C. setting the number of rows in the tableView.</p> <p>A.</p> <pre><code>-(void) setupFetchResultsController { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Project"]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSError *error; fetchedProjects = [managedObjectContext executeFetchRequest:request error:&amp;error]; if (fetchedProjects == nil) { // Handle the error. } self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil]; } </code></pre> <p>B.</p> <pre><code>- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { selectedProject = [self.fetchedResultsController objectAtIndexPath:indexPath]; [managedObjectContext deleteObject:selectedProject]; //setting up the fetch results controlleris intended to get the number rows correctly by fetching the existant projects. In the above log, the result would be 14 (not 13) without this line. Crashes with or without this line. [self setupFetchResultsController]; if (editingStyle == UITableViewCellEditingStyleDelete) { [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } </code></pre> <p>C.</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int noOfRows = [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; return noOfRows; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload