title: How to populate a core data store programmatically?
body: <p>Edit: a minimal project that exhibits the crash can be downloaded from <a href="http://muys.net/crashTest.zip" rel="nofollow noreferrer">crashTest</a>. It was created by choosing the "navigation based with core data" project template in XCode and modifying maybe ten lines.</p>
  
  <p>I have ran out of hairs to pull with a crash when a section and two objects are added in one go.</p>
  
  <p>The crash happens at the end of the routine inside the call to <code>[managedObjectContext save:&amp;error]</code>.</p>
  
  <p>The crash is an out-of-bound exception for an <code>NSArray</code>:</p>
  
  <pre><code>Serious application error.  Exception was caught during Core Data change processing: *** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)
  </code></pre>
  
  <p>Also maybe relevant, when the exception happen, my fetch result controller <code>controllerDidChangeContent:</code> delegate routine is in the call stack. It simply calls my table view <code>endUpdate</code> routine.</p>
  
  <p>I am now running out of ideas. How am I supposed to insert more than one item to a core data store with a table view using sections?</p>
  
  <p>Here is the call stack:</p>
  
  <pre><code>#0  0x901ca4e6 in objc_exception_throw
  #1  0x01d86c3b in +[NSException raise:format:arguments:]
  #2  0x01d86b9a in +[NSException raise:format:]
  #3  0x00072cb9 in _NSArrayRaiseBoundException
  #4  0x00010217 in -[NSCFArray objectAtIndex:]
  #5  0x002eaaa7 in -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:]
  #6  0x002def02 in -[UITableView endUpdates]
  #7  0x00004863 in -[AirportViewController controllerDidChangeContent:] at AirportViewController.m:463
  #8  0x01c43be1 in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]
  #9  0x0001462a in _nsnote_callback
  #10 0x01d31005 in _CFXNotificationPostNotification
  #11 0x00011ee0 in -[NSNotificationCenter postNotificationName:object:userInfo:]
  #12 0x01ba417d in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:]
  #13 0x01c03763 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:]
  #14 0x01b885ea in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:]
  #15 0x01bbe728 in -[NSManagedObjectContext save:]
  #16 0x000039ea in -[AirportViewController populateAirports] at AirportViewController.m:112
  </code></pre>
  
  <p>Here is the code to the routine. I apologize because a number of lines are probably irrelevant, but I'd rather err on that side. The crash happens when it calls <code>[context save:&amp;error]</code>:</p>
  
  <pre><code>- (void) insertObjects
  {
  NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
  NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
  
  NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
  
  // If appropriate, configure the new managed object.
  [newManagedObject setValue:@"new airport1" forKey:@"name"];
  [newManagedObject setValue:@"???" forKey:@"code"];
  [newManagedObject setValue:@"new country" forKey:@"country_name"];
  
  newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
  [newManagedObject setValue:@"new airport2" forKey:@"name"];
  [newManagedObject setValue:@"???" forKey:@"code"];
  [newManagedObject setValue:@"new country" forKey:@"country_name"];
  
  
  // Save the context.
  NSError *error = nil;
  if (![context save:&amp;error]) {
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
      abort();
  }
  }
  </code></pre>
  
  <p>Note: the sections are by <code>country_name</code>. Also, the four <code>NSFetchedResultsControllerDelegate</code> routines are as documented and as preset by XCode:</p>
  
  <pre><code>- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
  [self.tableView beginUpdates];
  }
  
  
  - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id &lt;NSFetchedResultsSectionInfo&gt;)sectionInfo
         atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
  {
  switch(type) {
      case NSFetchedResultsChangeInsert:
          [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
          break;
  // other cases omitted because not occurring in this crash            
  
  }
  }
  
  
  - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
    newIndexPath:(NSIndexPath *)newIndexPath
  {
  UITableView *tableView = self.tableView;
  
  switch(type) {
  
      case NSFetchedResultsChangeInsert:
          [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
          break;
  // other cases omitted because not occurring in this crash            
  }
  }
  
  
  - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
  [self.tableView endUpdates];
  }
  </code></pre>