Search code examples
realm

Easiest way to query nested optional realm objects in swift


I am using Realm for Swift. I have two Realm objects structures like this:

class outerObject : Object {
   dynamic var innerObject : innerInfo?
}

class innerInfo : Object {
  dynamic var sortId = 5
}

I want to make a query that gets every outerObject where the innerInfo is not nil and sortId equals 3. Normally I would use something like:

realm.objects(outerObject).filter(predicate)

And then write a suitable NSPredicate but I do not hink that works with optionals. So what is the recommended method?


Solution

  • This query should work: realm.objects(outerObject).filter("innerObject.sortId = 3").