views:

7

answers:

0

Hi everyone,

I have a model like this one:

Datamodel

I want to fetch the items in a grouped table view with the help of a NSFetchedResultsController where the Category.titles are the section titles, ordered with the help of Category.sortOrder.

I tried to implement a compare- (which compares the sortOrder property) and description-method (which returns the title property) for Category and init my fetchcontroller with:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"category" ascending:YES];

NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort, sort2, nil]]; RELEASE(sort); RELEASE(sort2);

fetchController = [[NSFetchedResultsController alloc] 
    initWithFetchRequest:fetchRequest 
    managedObjectContext:context
    sectionNameKeyPath:@"category" 
    cacheName:nil];
[fetchRequest release];

Unfortunately this solution works only after a context-update, the first fetch does not call my custom compare and description methods in Category class, so the section are neither sorted by sortOrder nor labeled with de description string.

Appreciate any hint how to solve such a fetch (in best practice)!