zic10 zic10 - 1 year ago 29
Objective-C Question

NSPredicate SUBQUERY for 1 to many to many relationship

I'm trying to build an NSPredicate to satisfy a given relationship. My data model has the following:

University -> College -> Classrooms - ClassroomType

  • A university object has 1 to many colleges.

  • A college object has 1 to many classrooms.

  • Each classroom has a given classroom type with a specified property
    i'm interested in.

That property is called
I want to build a query that can give me all universities that have a typeDescription equal to the value "computerlab". The objects that I have available to me are a list of Universities. I'm pretty sure that I'll need to construct an
but I'm not quite sure no how to build this to satisfy my requirements. If anyone has any input on the right way to build this query it would be much appreciated.

Answer Source

For one-to-many-to-many relationships, you need to nest two SUBQUERY clauses:

NSPredicate(format:"SUBQUERY(colleges, $c, SUBQUERY($c.classrooms, $room, $room.classroomType.typeDescription == %@).@count > 0).@count > 0","computer lab")