Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

You need to research following the To Many relationship through the NSSet that is created for you to represent the Route->>Day path in your database. Assuming you named your relationship isTraversedOn, and the day->Schedule as isScheduled, it would be something like:

Route *route=[self.fetchedResultsController.fetchedObjects objectAtIndex:0];
for (Day *day in route.isTraversedOn) {
   Schedule *schedule = day.isScheduled;
   NSLog(@"route is traversed on %@ according to schedule %@",day.name,schedule.name);
}

the NSSet just saves the actual id's of the indicated objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top