It seem realm does not respect && operator when filtering a list of embedded objects.
In this specific case it filter correctly key == "rooms" but second statement value == 4 seems to be ignored , once results brings rows with values different from 4 .
class Property: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name: String
@Persisted var specs: List<PropertySpec>
// ...
}
class PropertySpec: EmbeddedObject, ObjectKeyIdentifiable {
@Persisted var key: String
@Persisted var value: Double?
// ...
}
struct SearchResultView: View {
@ObservedResults(Property.self) var properties
var filteredProperties: Slice<Results<Property>> {
var results = properties
.where {
// It ignores && operator
// Actually it seems to consider any value == 4, not only "rooms"
$0.specs.key == "rooms" && $0.specs.value == 4
}
return results.prefix(50)
}
}
I am suspecting it will consider any spec.value with 4 .
Sample Data:
Property 1
specs:
rooms = 4
bathrooms = 3
Property 2
specs:
rooms = 3
bathrooms = 8
Property 3
specs:
rooms = 6
bathrooms = 4 // <---- suspect here!
I also tried this, but it does not work. The list will be empty.
I still think the .key property is causing the issue but one other thing to consider (which you may have already considered) is to leverage the Realm Map property as it really seems well suited for this use case and isn’t a workaround.
So your Model would look like this
class Property: Object {
@Persisted var name: String
@Persisted var propertySpecs: Map<String, Int>
}
And then the Map property could contain any spec needed rooms, bathrooms, etc and could be queried like this