[Realm Multiplatform - queries] Hello, we are tryi...
# realm
p
[Realm Multiplatform - queries] Hello, we are trying to rewrite our android realm database into multiplatform, We have faced a small issue with one of our uese cases. We have a object "Group" that has a RealmList<User> field. We would like to get these Users from this list with some extra filtering that is done on database. Any ideas what would be the best soultion here?
n
Can you share your model, and the old query (using the Java SDK)?
p
class Group : RealmObject(){
@PrimaryKey
var id:String=""
var users:RealmList<User>
}
class User : RealmObject(){
@PrimaryKey
var id:String=""
var age:Int?=null
var name:String?=null
}
Usage:
groupIntance.users.where()...
Unforunately at the moment the multiplatform RealmList is not
Queryable<T>
at the moment
n
You can still query by
Group
but specify a filter by the RealmList as follow:
Copy code
val results: RealmResults<Group> =  realm.objects(Group::class)
        .query("users.age > $0 AND users.name BEGINSWITH $1", 19, "user")
Obviously you can also query directly
User
Copy code
val result_users: RealmResults<User> =  realm.objects(User::class)
        .query("age > $0 AND name BEGINSWITH $1", 19, "user")
see https://github.com/realm/realm-kotlin#query check more query examples here https://docs.mongodb.com/realm/sdk/node/advanced/query-engine/#comparison-operators
p
Ahh, maybe I was unclear with the issue description. We want to filter users from within the group. First solution would give us, a list of Groups with all users, and we need a subset. Second solution would give us all users, but we want users that are in the list of specified group. We were thinking on something like:
Copy code
val result_users: RealmResults<User> =  realm.objects(User::class)
        .query("age > $0 AND name BEGINSWITH $1 AND id IN $2", 19, "user",group.users)
But this is not working anyhow.
n
we want users that are in the list of specified group
This could be possible using LinkingObjects which are not yet implemented in Realm Kotlin One work around could be to first query the needed `Group`s then apply Kotlin Collections filter to iterate over each group users to filter out the one that satisfies your conditions
p
Ahh, ok then we will stick with Kotlin Collections. Thank you 🙂
100 Views