views:

185

answers:

1

I have a one to many entity relationship between two entities:

EntityP (Parent) <-->> EntityC (Child)

Attributes and Relationships:

EntityP.title
EntityP.dateTimeStamp
EntityP.PtoC (relationship)

EntityC.title
EntityC.dateTimeStamp
EntityC.CtoP (relationship) // Can be used to get "one" EntityP record

I use fetch results controller to show the results. Here's my implementation of the fetch results controller:

#pragma mark -
#pragma mark Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController {
     // Set up the fetched results controller if needed
     if (fetchedResultsController != nil) {
          return fetchedResultsController;
     }

     // Create the fetch request for the entity
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

     // Set Entity
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityC" inManagedObjectContext:self.managedObjectContext];
     [fetchRequest setEntity:entity];

     // Set Predicate
     // (Ignore, we want to get list of all EntityC records)

     // Set Sort Descriptors (sort on Parent - for section, and then on Child - for rows)
     NSSortDescriptor *sortDescriptorPDate = [[NSSortDescriptor alloc] initWithKey:@"CtoP.dateTimeStamp" ascending:YES];
     NSSortDescriptor *sortDescriptorDate = [[NSSortDescriptor alloc] initWithKey:@"dateTimeStamp" ascending:YES];
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorPDate, sortDescriptorDate, nil];

     [fetchRequest setSortDescriptors:sortDescriptors];
     [fetchRequest setFetchBatchSize:20];

     // Create and initialize the fetch results controller
     NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"CtoP.title" cacheName:nil];
     aFetchedResultsController.delegate = self;
     self.fetchedResultsController = aFetchedResultsController;

     // Cleanup memory
     [aFetchedResultsController release];
     [fetchRequest release];
     [sortDescriptorPDate release];
     [sortDescriptorDate release];
     [sortDescriptors release];

     NSError *error = nil;
     if (![fetchedResultsController performFetch:&error]) {
          /*
           Replace this implementation with code to handle the error appropriately.

           abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
           */
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          abort();
     }

     return fetchedResultsController;
}     

Now, for example, if i have following data in the persistence store:

EntityP.dateTimeStamp  EntityP.title    EntityC.dateTimeStamp   EntityC.title
Today                  B                Today                   d
Yesterday              A                Yesterday               a
Today                  B                Yesterday               c
Yesterday              A                Today                   b

Note: Yesterday and Today is in NSDate format.

Then i should get the sections and rows in following order (exactly):

A
 a
 b

B
 c
 d

But, the sort is not working like this. I'm getting the rows in correct order, but the sections are not ordered! I hope sortDescriptorPDate is doing his job. What am i missing? Thanking in anticipation.

A: 

Not sure I understand your setup but...

By default the section titles are the capitalized first letter of the section name. If you want a custom sections such as ones based on dates, you will need to subclass NSFetchedResultsController and override the various sectionIndex... methods to return the correct sections, section indexes and section titles.

TechZen
The problem is not with the section title, it's with their sort order.
Mustafa