kotlin-extensions / io.realm.kotlin / io.realm.RealmResults / toFlow


fun <T : RealmModel> RealmResults<T>.toFlow(): Flow<RealmResults<T>>

Returns a Flow that monitors changes to this RealmResults. It will emit the current RealmResults when subscribed to. RealmResults will continually be emitted as the RealmResults are updated - onCompletion will never be called.

Items emitted from Realm flows are frozen - see RealmResults.freeze. This means that they are immutable and can be read from any thread.

Realm flows always emit items from the thread holding the live RealmResults. This means that if you need to do further processing, it is recommended to collect the values on a computation dispatcher:

  .map { results -> doExpensiveWork(results) }
  .onEach { flowResults ->
    // ...

If your would like toFlow() to stop emitting items you can instruct the flow to only emit the first item by calling kotlinx.coroutines.flow.first:

val foo = realmInstance.where(Foo::class.java)

Kotlin Flow on which calls to onEach or collect can be made.