views:

515

answers:

1

I am familiar with the common and basic use of a NSFetchedResultsController managing the rows of a table, but I have an interesting problem.

My data is organized as such: Schedule <--->> Day <--->> Route. I currently have a table view that has Schedules with a fetch controller managing the creation and deletion of Schedules. I now want to create a table view for Routes/Days.

I would like to list a Route on every row, and create custom section headers that correspond to information within the relevant Day. I could probably hack something together by fetching all the routes, and then sectioning them by the inverse relationship to there respective Day, but I am worried that then I will not be able to take advantage of the fetch controller delegate methods for updating the table when managed objects change.

Does anyone have any experience with this? Any ideas on how to move forward?

Thanks.

+1  A: 

The NSFetchedResultsController is designed for this purpose, so you don't have to invent the wheel.

When you initialize your NSFetchedResultsController, you should consider specifying the sectionNameKeyPath. That property is exactly what you need to automatically generating the section for you.

In your case, I would do as follows:

NSEntityDescription: @"Route"
NSPredicate: @"day = %@"
NSFetchedResultsController: sectionNameKeyPath:@"route.day"

You see what I mean, if you need more code for clarification, tell me.

sfa
Is there any simple possibility to get an object instead of the sectionName from the `NSFetchedResultsController`? I'm building a custom section header from the `NSFetchedResultsController` using my object (the keyPath is in fact a "group by <tour>"): id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; Tour *tour = (Tour *)[[[sectionInfo objects] objectAtIndex:0] tour];Is there any simpler way, or is this the above code the only possibility for this?
ComSubVie
sfa
Yes, the above snippet is used in the titleForHeaderInSection: method of the DataSource. The question is if there is any simpler method to get the object by which the sectionNameKeyPath groups, because if this is a Core Data relationship and not an attribute, it is a string (description) of the object, and not the object itself - and I need to get the object with the above code, which is not really straightforward.
ComSubVie
you can then point directly to the relationship.attribute in the sectionNameKeyPath to get those objects
sfa
But this is still a string and no object or did I miss something?
ComSubVie
the question is why a string can not be used as the sectionNameKeyPath? have you tried to do it?
sfa