I have a simple object graph with person and item entities. I have a one to many relationship from person to items and the inverse. I want to show in a tableview all of the items that belong to the selected person using a fetched results controller so I don't have to deal with updating the table views when I add and delete etc. How can I do this? It's driving me crazy. Thanks!
1 Answers
0
votes
I assume you are ok with setting up and using NSFetchedResultsController in general or are happy to refer to the Apple sample code and documentation? So I'm guessing you are asking how to construct an NSFetchRequest to fetch the Item objects for a particular person which you can then pass to the fetched results controller? If so here is an example of how it's done.
Person *person1 = [self someMethodThatReturnsTheRightPersonObject];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
// Assumes the inverse relationship Item -> Person is named "person"
request.predicate = [NSPredicate predicateWithFormat:@"person==%@", person1];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"someKindOfOrderingAttibute" ascending:YES]];