Search code examples
swiftrealm

Filtering LinkingObjects of a LinkingObject in a predicate in RealmSwift


Given the following example realm classes:

Car

class Car: Object {
   dynamic var make = ""
   let owner = List<Person>()
}

Person

class Person: Object {
   dynamic var name = “”
   dynamic var age = 0
   let children = List<Person>()
   let dad = LinkingObjects(fromType: Person.self, property: "children")
   let cars = LinkingObjects(fromType: Car.self, property: "owner")
}

I'd like to get the people whose dad drives a Mustang (using a predicate).

I would think of a predicate like this one:

 "(ANY dad[FIRST].cars.make  == Mustang)"

But [FIRST] is not supported yet.

Is there a another way to achieve this in just one predicate?


Solution

  • To find people whose father drives a mustang, you can use a predicate like so:

    realm.objects(Person.self).filter("ANY dad.cars.make = 'Mustang'")