Pieter
Pieter

Reputation: 1831

How can I use NSFetchedResultsController and custom sections?

My app has a list of schedules.

I'm currently using sectionNameKeyPath:@"source", which is either doctor or personal, problem is the expired section. This can be determined by an endDate attribute in that same entity.

NSFetchedResultsController *aFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext
                                        sectionNameKeyPath:@"source" cacheName:@"Root"];

So how can I keep using NSFetchedResultsController but somehow use these three sections?

Upvotes: 2

Views: 1407

Answers (1)

Amy Worrall
Amy Worrall

Reputation: 16337

In your data model, make a new transient attribute on your Schedule entity, called "sectionName". Then write a method on your Schedule class -(NSString*)sectionName, to return the correct string. Finally use @"sectionName" as the section name key path for your fetched results controller.

Don't forget that in your fetch request, the items have to be already sorted into the right order to be grouped into sections — that's a requirement of the NSFetchedResultsController.

Upvotes: 3

Related Questions