对象内部对象的领域谓词

realm predicate with an object inside object

我有以下领域对象

class Patient: Object {

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

class RString: Object {

    @objc dynamic var stringValue: String?

}

我需要使用 stringValue = "test"

过滤 Patient 列表中具有 RString 组件的对象

这样的事情可能吗?

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

您需要使用 SUBQUERY 才能访问 NSPredicateList 的元素的属性。 SUBQUERY 将为每个 Patient 计算 true,其 list 属性 包含至少 1 个 RString 元素,其 stringValue 匹配提供的String.

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