title: The fetched object at index X has an out of order section name 'X. Objects must be sorted by section name'
body: <p>I know this has been covered in various questions, but I can't figure this one out.</p>
  
  <p>As far as I can tell my setup is correct. My first sort descriptor matches the <code>sectionNameKeyPath</code> exactly.</p>
  
  <p>My customers get this crash very rarely, perhaps because it isn't on a screen that is typically open for that long. Because the crash is rare, I haven't been able to reproduce it at all.</p>
  
  <p>It crashes on viewWillAppear->table reloadData->numberOfSectionsInTableView->accessor for nsfetchedresultscontroller perform fetch.</p>
  
  <p>It's always the same error message recorded to my crash reporter, with different numbers: 
  <strong>The fetched object at index 9 has an out of order section name '2. Objects must be sorted by section name'</strong></p>
  
  <p>I use the statusId field, which is always a number of 1-5.</p>
  
  <p>Code:</p>
  
  <pre><code>- (NSFetchedResultsController *)unitsFetchedResultsController
  {
      if (unitsFetchedResultsController != nil) {
          return unitsFetchedResultsController;
      }
  
      // SELECT * from Unit
      NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"Unit" inManagedObjectContext:self.guestCard.managedObjectContext];
      [fetchRequest setEntity:entity];
      // make this more defensive by limiting the status ids (1-14ish are valid on the backend, we only display these) 
      NSPredicate *availablePredicate = [NSPredicate predicateWithFormat:@"available = 1"];
      [fetchRequest setPredicate:availablePredicate];
      // ORDER BY ready DESC, availableDate ASC  (ATGT - must sort by 'ready' first or else 'sectionNameKeyPath' in the NSFetchedResultsController won't work)
      NSSortDescriptor* readySortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"statusId" ascending:YES];
      NSSortDescriptor* bedroomsSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"floorplan.bedrooms" ascending:YES];
      NSSortDescriptor* bathroomsSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"floorplan.baths" ascending:YES];
      NSSortDescriptor* availableDateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"availableDate" ascending:YES];
      [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:readySortDescriptor, bedroomsSortDescriptor, bathroomsSortDescriptor, availableDateSortDescriptor, nil]];
  
      NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.guestCard.managedObjectContext sectionNameKeyPath:@"statusId" cacheName:@"unitsAvailable"];
      aFetchedResultsController.delegate = self;
      self.unitsFetchedResultsController = aFetchedResultsController;
  
      // Clean up
      [aFetchedResultsController release];
      [fetchRequest release];
  
      NSError *error = nil;
      if (![[self unitsFetchedResultsController] performFetch:&amp;error]) {
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          [CoreDataErrorWriter writeCoreDataErrorToFile:error];
  
          perimeterAppDelegate *appDelegate = APP_DELEGATE;
          [appDelegate showCoreDataError];
  
          [NSException raise:@"Core Data Error" format:@"Core Data Error in %@", NSStringFromClass([self class])];
      }
  
      return self.unitsFetchedResultsController; // I recognize I should just return the iVar, but not the problem.
  }
  </code></pre>