0
votes

I'm using CoreData and NSFetchedResultsController in my project. I have 2 entities School & Students, one to many relationship.. a school can have multiple students.

I'm trying to filter schools by school name and then filter students by grade in such a way that i get a Filtered School that contains ONLY the students that matches certain grade.

    let fetchRequest = NSFetchRequest<School>(entityName: "School")
    let predicate = NSPredicate(format: "name = %@ AND ANY students.grade","School 2", "F")

The issue with the above mentioned predicate code is that it returns me a "School 2" with ALL the students whereas only one student has "F" grade.

I need a School entity that should contain ONLY the student with "F" grade.

1

1 Answers

0
votes

The school object that you fetch will always give all values that it has in its relationship unrelated to how you fetched the object. What you can do is filter the students. So you have a set school.students and the make a second set that only contains failing students. Because the filtering of the students is in memory you are not limited to core data properties, but it may also be a bit slower.