Search code examples
kotlinandroid-studioandroid-jetpack-composerealmrealm-mobile-platform

How can I read data from a Realm Query in Jetpack Compose?


I have a User query with data and I want to retrive the data inside it, but I can't do it, in the console log all it says me is this ,, StandaloneCoroutine{Completed}@55d57e3 "

I want to retrive the data and then make operations on it, can anyone help?? I tried other solutions but nothing worked....

UPDATE

So I changed to a Realm List but I cant access the elements inside the User query, how can I do that?

class User: RealmObject {
@PrimaryKey var _id: ObjectId = ObjectId()
var email: String = ""
var password: String = ""
var code: String = ""

}

override val getUsers: Flow<RealmList<User>>
    get() = realm.query<User>()
        .find()
        .asFlow()
        .map {results->
        results.list.toRealmList()
    }

Solution

  • The answer kind of depends on what the use case is - but there are several issues with the code in the question.

    In general, when working with Realm, best practice is to use RealmList instead of Kotlin List. It works similar but provides a Realm layer to interact with objects.

    However, it doesn't appear you working with either based on the return.

    There's a query with a .find and that process will actually return a RealmResults object (which is immutable by the way) which is then what you should be returning from the function.