title: Deleting row: error 'Invalid update: invalid number of rows in section 0'
body: <p>11-24-2013: I did some more debugging, what I found out is that removeProject is working fine. (I have printed all projects before and after removing) Only when it comes back in -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section the count is 0 instead of (total - 1).</p>
  
  <p>============</p>
  
  <p>I am stuck on this error for a while now. To understand Core Data better, I have created this test project where you can enter a client and his projects. The projects are related to the client.
  For projects I have copied the ViewController of Clients and made some minor changes. 
  I am able to enter several Clients and then delete them. The problem starts when I want to delete related projects. If there is a client with only one project, I can delete this without any error. If a client has two or more projects, I am not able to delete any projects from this client. 
  Deleting will give me this error:</p>
  
  <pre><code>2013-10-30 10:00:23.145 [6160:70b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1330
  2013-10-30 10:00:23.147 [6160:70b] *** 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 (2), 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).'
  *** First throw call stack:
  (
  
   .....
       )
       libc++abi.dylib: terminating with uncaught exception of type NSException
       Program ended with exit code: 0 
  </code></pre>
  
  <p>After reading a lot of answers on this site, My guess is that I need to change my code in
       numberOfRowsInSection method, but I do not see what to change there.</p>
  
  <p>My code: </p>
  
  <p>I have created a Datastore:</p>
  
  <p>DataStore.m</p>
  
  <pre><code>    #import "BITDataStore.h"
      #import "Client.h"
      #import "Project.h"
  
      @implementation DataStore
  
  
  + (BITDataStore *)sharedStore
  {
      static BITDataStore *sharedStore = nil;
      if (!sharedStore) 
          sharedStore = [[super allocWithZone:nil]init];
  
          return sharedStore;
  }    
  
  -(void)removeClient:(Client *)client
  {
      // remove from NSManagedObjectContext
      [context deleteObject:client];
  
      // remove from allClients array
      [allClients removeObjectIdenticalTo:client];
      NSLog(@"remove client");
  }
  
  -(void)removeProject:(Project *)project
  {
      // remove from NSManagedObjectContext
      [context deleteObject:project];
  
      // remove from allProjects array
      [allProjects removeObjectIdenticalTo:project];
  
      // remove from relatedProjects array 
      [relatedProjects removeObjectIdenticalTo:project];
      NSLog(@"remove project %@", [project project]);
  
  }
  
  
  -(NSArray *)relatedProjects:(Client *)client;
  {
      NSFetchRequest *request = [[NSFetchRequest alloc]init];
  
      NSEntityDescription *e = [[model entitiesByName] objectForKey:@"Project"];
  
      [request setEntity:e];
  
      // Check if client is related to Project
      [request setPredicate: [NSPredicate predicateWithFormat:@"clients == %@", client.objectID]];
  
      NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"project"
                                                           ascending:YES];
      [request setSortDescriptors:[NSArray arrayWithObject:sd]];
  
      NSError *error;
      NSArray *result = [context executeFetchRequest:request error:&amp;error];
  
      if (!result) {
          [NSException raise:@"Fetch failed"
                      format:@"Reason: %@", [error localizedDescription]];
      }
      relatedProjects = [[NSMutableArray alloc] initWithArray:result];
  
      return relatedProjects;
  }   
  @end
  </code></pre>
  
  <p>ProjectDataViewController.m</p>
  
  <pre><code>@synthesize client, project;
  
  
  -(void)viewWillAppear:(BOOL)animated
  {
      [super viewWillAppear:animated];
      [[self tableView] reloadData];
  }
  
  
  #pragma mark - Table view data source
  
  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      return [[[BITDataStore sharedStore]relatedProjects:client]count];        
  }
  
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  
      // Configure the cell...
      if (cell == nil){
          cell = [[UITableViewCell alloc]
                  initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
      }
  
     Project *p = [[[BITDataStore sharedStore]relatedProjects:client]
                                                objectAtIndex:[indexPath row]];
  
      [[cell textLabel] setText:[p project]];
  
      return cell;
  }
  
  // Override to support editing the table view.
  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  {
      if (editingStyle == UITableViewCellEditingStyleDelete) {
          BITDataStore *ds = [BITDataStore sharedStore];
          NSArray *selectedProjects = [ds relatedProjects:client];
  
          Project *pr = [selectedProjects objectAtIndex:[indexPath row]];
          NSLog(@"Deleting project %@", [pr project]);
         [ds removeProject:pr];
  
          // Delete the row from the data source
          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
          // Finally, reload data in view
          [[self tableView] reloadData];
          NSLog(@"Reload data"); 
      }   
      else if (editingStyle == UITableViewCellEditingStyleInsert) {
          // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
      }   
  }
  
  @end
  </code></pre>
  
  <p>Hopefully this is enough information, if not please let me know. </p>