Question

I use coredata with this layout

enter image description here

Now I would like to show a table with the Cave.title as rows ordered in the Region.region as section. With the following code it shows the sections and the rows correctly. Only problem is that the order is not right. The rows below the sections don't belong to that section.

NSManagedObjectContext *context = country.managedObjectContext;

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Cave" inManagedObjectContext:context];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
    request.predicate = [NSPredicate predicateWithFormat:@"country = %@", country];
    request.fetchBatchSize = 20;

    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
                                       initWithFetchRequest:request
                                       managedObjectContext:context
                                       sectionNameKeyPath:@"region.region"
                                       cacheName:nil];

    [request release];

    self.fetchedResultsController = frc;

I am not sure which part of the tableviewcontroller I should show to you?

enter image description here

"Bätterich" should be in "Others" and "Pertusio Acquacalda" should be in "Ticino" I have checked Core Data's sql file and the references are right in there.

Was it helpful?

Solution

Solved the problem by using the following sortdescriptors

NSSortDescriptor *sortDescriptorRegion = [[NSSortDescriptor alloc] initWithKey:@"region.region" ascending:YES];
NSSortDescriptor *sortDescriptorCave = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorRegion, sortDescriptorCave, nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top