title: coredata sqlite malformed DB
body: <p>I have problems with my coredata sqlite DB, which hosts a book DB. After a crash a user experiences has the problem, that the data isn't shown properly any more in his/her tableview.</p>
  
  <p>This is due to the fact, that the performFetch method returns an error:</p>
  
  <pre><code>[NSFetchedResultsController deleteCacheWithName:nil];
  if (![[self fetchedResultsController] performFetch:&amp;error]) { 
          //NSArray *array = [[self fetchedResultsController] fetchedObjects];
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  #ifdef DEBUG
          abort();
  #endif
      }
  </code></pre>
  
  <p>which results in this error message:</p>
  
  <p>Unresolved error Error Domain=NSCocoaErrorDomain Code=134060 "The operation couldn’t be completed. (Cocoa error 134060.)" UserInfo=0x1dff80 {reason=The fetched object at index 312 has an out of order section name 'Z. Objects must be sorted by section name'}, {
      reason = "The fetched object at index 312 has an out of order section name 'Z. Objects must be sorted by section name'";</p>
  
  <p>When I have a look into the sqlite file with 'SQLite Database Browser 2.0 b1' the attributes of each entity seem to be ok.</p>
  
  <p>When I delete some of the entities being mentioned everything works fine again.</p>
  
  <p>I would like to know how I can find out what exactly is wrong with the mentioned entities and fix that, so the user can use his/her data again. Of course I want to fix the bug which causes the malformed DB as well but that is out of focus in this post.</p>
  
  <p>Does anybody have any hints where I could have a look at or what might be malformed within my DB or what "an out of order section name" is?</p>
  
  <p>This is the code for my fetchedResultsController:</p>
  
  <pre><code>- (NSFetchedResultsController *)fetchedResultsController {
  
      if (fetchedResultsController != nil) {
          return fetchedResultsController;
      }
  
  
      // Create the fetch request for the entity.
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
      // Edit the entity name as appropriate.
      NSEntityDescription *entity;
      entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:[[GlobalData sharedInstance] managedObjectContext]];
      [fetchRequest setEntity:entity];
  
      // Set the batch size to a suitable number.
      [fetchRequest setFetchBatchSize:10];
  
      //set searchPredicate
      NSPredicate *predicate = nil;
      if (self.bibList != nil) {
          predicate = [NSPredicate predicateWithFormat:@"ANY BibLists.name LIKE %@", self.bibList.name];      
      }
  
      if (predicate) {
          [fetchRequest setPredicate:predicate];
      }
  
  
      // Edit the sort key as appropriate.
      NSSortDescriptor *sortDescriptor;   
      NSString *sortDescriptorString = nil;       
      sortDescriptorString = @"title";
      sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescriptorString ascending:YES]; 
      NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];   
      [fetchRequest setSortDescriptors:sortDescriptors];
  
      NSString *sectionNameKeyPath = nil;
      sectionNameKeyPath = @"uppercaseFirstLetterOfTitle";
  
      // Edit the section name key path and cache name if appropriate.
      // nil for section name key path means "no sections".
      NSFetchedResultsController *aFetchedResultsController;
      aFetchedResultsController = [[NSFetchedResultsController alloc] 
                               initWithFetchRequest:fetchRequest 
                               managedObjectContext:[[GlobalData sharedInstance] managedObjectContext] 
                               sectionNameKeyPath:sectionNameKeyPath 
                               cacheName:@"Bibliography"];
  
      aFetchedResultsController.delegate = self;
      self.fetchedResultsController = aFetchedResultsController;
  
      [aFetchedResultsController release];
      [fetchRequest release];
      [sortDescriptor release];
      [sortDescriptors release];
  
      return fetchedResultsController;
  }    
  </code></pre>
  
  <p>Thanks
  b00tsy</p>