Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController and Relationship

This time I get a strange behavior with NSFetchedResultsController. I create a fetchRequest like this:

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 with executeFetchRequest:error: of NSManagedObjectContext, I get the expected Result. All Exams according to the student. (Between Student and exam is a one-to-many relationship) But If I use the same fetchRequest in a NSFetchedResultsController, I get something different. Until now I didn't get out, what I exactly get. In my eyes the result is random.

Can you help me? I want to manage the exams of a given student with a NSFetchedResultsController.

Sandro Meier

like image 782
Sandro Meier Avatar asked Nov 14 '22 19:11

Sandro Meier


1 Answers

If you have a Student object already in hand, you don't have to fetch the Exam objects you just ask the Student object for the contents of its exams relationship. There is no need to fetch because you already have a reference to all the Exam objects you want.

As to why the fetch works outside the fetch results controller, I can't say with certainty. The controller does nothing but take the results of a fetch and package them for display in a tableview. If the data does not display properly in the tableview, then the problem is most likely in the tableview delegate/datasource methods where you connect the contents of the fetched results controller to tableview.

like image 69
TechZen Avatar answered Dec 05 '22 08:12

TechZen