title: fetchedresultscontroller behavior
body: <p>I have a table view that is sorted by a segmented switch.  I want to sort the table by an NSNumericSearch so that I get 1,2,11,22 instead of 1,11,2,22. I'm using Core Data.</p>
  
  <p>If I create multiple records with the 'color' property remaining the same (ie.all 'red'), while varying the length, the sort for length initially is correct (1,2,11,22). If I then switch to another selectedSegmentIndex of the segmented control, such as 'shape' or 'length', I get an erroneous sort order (1,11,2,22) on the same data.</p>
  
  <p>I thought the fetchedResultsController was supposed to recompute the data with a call for the tableView to reload data.</p>
  
  <p>I am not using a predicate.  I have tried setting the cache name to nil with no effect. I have also tried deleteCacheWithName: although it may need to be in a different location.</p>
  
  <p>Some code:</p>
  
  <pre><code>- (NSFetchedResultsController *)fetchedResultsController{    
  if (__fetchedResultsController != nil) {
      return __fetchedResultsController;
  } 
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:self.managedObjectContext];
  [fetchRequest setEntity:entity];
  
  // Set the batch size to a suitable number.
  [fetchRequest setFetchBatchSize:20];
  
  switch (segmentedControl.selectedSegmentIndex) {
      case 0:
          sortKey = @"color";
          break;
      case 1:
          sortKey = @"length";
          break;
      case 2:
          sortKey = @"smell";
          break;
      case 3:
          sortKey = @"shape";
          break;
      default:
          sortKey = @"color";
          break;
  }
      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:YES comparator:^(id obj1, id obj2) { return [obj1 compare:obj2 options:NSNumericSearch]; }];
  NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"length" ascending:YES comparator:^(id obj1, id obj2) { return [obj1 compare:obj2 options:NSNumericSearch]; }];
  NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor,sortDescriptor2, nil];
  
  [fetchRequest setSortDescriptors:sortDescriptors];
  
  NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:sortKey cacheName:@"Master"];
  
  aFetchedResultsController.delegate = self;
  self.fetchedResultsController = aFetchedResultsController;
  
  NSError *error = nil;
  if (![self.fetchedResultsController performFetch:&amp;error]) {
      abort();
  }
  return __fetchedResultsController;
  }
  - (IBAction)changeSegment:(id)sender {
  switch (segmentedControl.selectedSegmentIndex) {
      case 0:
          sortKey = @"color";
          break;
      case 1:
          sortKey = @"length";
          break;
      case 2:
          sortKey = @"smell";
          break;
      case 3:
          sortKey = @"shape";
          break;
      default:
          sortKey = @"one";
          break;
  }
  self.fetchedResultsController = nil;
  [self.tableView reloadData];
  //[NSFetchedResultsController deleteCacheWithName:@"Master"];
  }
  </code></pre>
  
  <p>Can anyone help me on this behaviour of first time correct, second time incorrect sorting?</p>