Anya Alstreim
Anya Alstreim

Reputation: 153

realm predicate with an object inside object

I have the following Realm Objects

class Patient: Object {

    @objc dynamic var name: String?
    let list = List<RString>()
}

class RString: Object {

    @objc dynamic var stringValue: String?

}

I need to filter Patient objects that have an RString component in List with stringValue = "test"

Is something like this possible?

patients = realm?.objects(Patient.self).filter("name = 'name1' AND @% IN list", RString(stringValue: 'test'))

Upvotes: 1

Views: 863

Answers (1)

David Pasztor
David Pasztor

Reputation: 54716

You need to use a SUBQUERY to be able to access the properties of the elements of a List in an NSPredicate. The SUBQUERY will evaluate true for every Patient whose list property includes at least 1 RString element whose stringValue matches the provided String.

patients = realm?.objects(Patient.self).filter("name = %@ AND SUBQUERY(list,$element,$element.stringValue == %@).@count>0", "name1", "test")

Upvotes: 2

Related Questions