Kotlinx.coroutines: How to emit data to kotlin flow

Created on 19 Aug 2019  路  3Comments  路  Source: Kotlin/kotlinx.coroutines

How can I emit new data to an existing Kotlin Flow? Currently I can only do it inside the flow block like this:

val data: Flow<Boolean> = flow {
    emit(newData)
}

I want to do something like:

fun someMethod() {
    data.emit(newData)
}

I can do it if I use Android LiveData:

val data: LiveData<Boolean> = MutableLiveData()

fun someMethod() {
    data.postValue(newData) // data.value = newData
}

or something likes it in RxJava2 with onNext event

Most helpful comment

The Flow analogue of LiveData is being developed in PR #1354. Right now you can use ConflatedBroadcastChannel.

All 3 comments

Afaik Flow is designed to be a self contained, replayable, cold stream, so emission from outside of it's own scope wouldn't be part of the contract. I think what you're looking for is a Channel.

Thanks @ashdavies

The Flow analogue of LiveData is being developed in PR #1354. Right now you can use ConflatedBroadcastChannel.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mvysny picture mvysny  路  3Comments

IgorKey picture IgorKey  路  3Comments

mariusstaicu picture mariusstaicu  路  3Comments

mhernand40 picture mhernand40  路  3Comments

mgj picture mgj  路  3Comments