NSFetchedResultsController and Relationships

This time I get weird behavior with NSFetchedResultsController. I create fetchRequest as follows:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entdesc = [NSEntityDescription entityForName:@"Exam" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entdesc];
NSPredicate *predi = [NSPredicate predicateWithFormat:@"student == %@", self.student];
[fetchRequest setPredicate:predi];

If I execute it using executeFetchRequest:error:NSManagedObjectContext, I get the expected result. All student exams. (One-to-many relationship between student and exam) But if I use the same fetchRequest in NSFetchedResultsController, I get something else. So far I have not got out, which I understood for sure. In my eyes, the result is random.

Could you help me? I want to manage this studentโ€™s exams using NSFetchedResultsController.

Sandro Meyer

+5
source share
1 answer

If you already have an object Student, you do not need to retrieve the objects Examthat you simply request for the object Studentfor the contents of its relationship exams. There is no need to retrieve, because you already have a link to all the objects Examyou want.

As to why the sample works outside the sample results controller, I cannot say for sure. The controller does nothing, but accepts the results of the selection and packs them for display in a table. If the data does not display properly in the table view, then the problem is most likely in the delegate / data source methods of tableview, where you connect the contents of the resulting result controller to tableview.

+1
source

All Articles