fun <T> RealmList<T>.toFlow(): Flow<RealmList<T>>
Returns a Flow that monitors changes to this RealmList. It will emit the current
RealmResults when subscribed to. RealmList updates will continually be emitted as the RealmList
is updated - onCompletion
will never be called.
Items emitted from Realm flows are frozen - see RealmList.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 RealmList. This means that if you need to do further processing, it is recommended to collect the values on a computation dispatcher:
list.toFlow()
.map { list -> doExpensiveWork(list) }
.flowOn(Dispatchers.IO)
.onEach { flowList ->
// ...
}.launchIn(Dispatchers.Main)
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 = list.toFlow()
.flowOn(context)
.first()
Return
Kotlin Flow on which calls to onEach
or collect
can be made.