I have three objects nested via lists like this:
class Canteen: Object {
dynamic var name: String?
let lines = List()
}
class L
Realm doesn't have any sort of concept of a deep-filtered view, so you can't have a Results which restricts the Lists contained in related objects to vegan meals.
There are several similar things which you can do. You could add inverse relationship properties, and then query Meal objects instead:
class Canteen: Object {
dynamic var name: String?
let lines = List()
}
class Line: Object {
dynamic var name: String?
let meals = List()
let canteens = LinkingObjects(fromType: Canteen.self, property: "lines")
}
class Meal: Object {
dynamic var name: String?
dynamic var vegan: Bool = false
let lines = LinkingObjects(fromType: Line.self, property: "meals")
}
let meals = realm.objects(Meal).filter("vegan = true AND ANY lines.canteens.name = %@", selectedCanteenType.rawValue)
(Or rather, you will be able to once Realm 0.102.1 is out; currently this crashes).
If you just need to iterate over the meals but need to do so from the Canteen down, you could do:
let canteens = realm.objects(Canteen).filter("name = %@ AND ANY lines.meals.vegan = true", selectedCanteenType.rawValue)
for canteen in canteens {
for line in canteen.lines.filter("ANY meals.vegan = true") {
for meal in line.meals.filter("vegan = true") {
// do something with your vegan meal
}
}
}
This unfortunately has some duplication due to needing to repeat the filter for each level of the references.