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
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.
Most helpful comment
The Flow analogue of LiveData is being developed in PR #1354. Right now you can use
ConflatedBroadcastChannel.