Colton Idle
06/03/2022, 8:08 AMephemient
06/03/2022, 9:34 AMservice.flow1().mapLatest { id ->
coroutineScope {
service.flow2(id).launchIn(this)
service.flow3(id).launchIn(this)
}
}.buffer(0).launchIn(viewModelScope)
which will re-start flow2
, flow3
with each id
from flow1
. (or else what do you expect to happen before an id
is available, or after a new value arrives?)Colton Idle
06/03/2022, 10:24 AMviewModelScope.launch {
val initialValue = service.flow1().first().someProperty
launch { service.flow1().collect { //update UI with changes } }
launch { service.flow2(initialValue).collect { //update UI with changes} }
launch { service.flow3(initialValue).collect { //update UI with changes} }
}
Colton Idle
06/03/2022, 10:28 AMservice.flow1().mapLatest { id ->
coroutineScope {
service.flow2(id).launchIn(this)
service.flow3(id).launchIn(this)
}
}.buffer(0).launchIn(viewModelScope)
wouldn't work is because I don't know where I would do the //update UI with changes for flow1() since the result of Flow1 is a complex model, where I just need one property from that complex model to start flow2 and flow3.Francesc
06/03/2022, 3:27 PMflow
into a shared flow after you map it to the id
, then use that shared flow to drive your flow2
and flow3
, something like this
val idFlow = flow1.map { value ->
// do whatever processing you need with value here
value.id
}.distinctUntilChanged()
.shareIn(viewModelScope, SharingStarted.Eagerly)
idFlow.flatMapLatest { id ->
flow2(id)
}.onEach { value ->
// handle value
}.launchIn(viewModelScope)
idFlow.flatMapLatest { id ->
flow3(id)
}.onEach { value ->
// handle value
}.launchIn(viewModelScope)
Francesc
06/03/2022, 3:38 PMflow
shared and then launch 3 collectors on that
val sharedFlow = flow1
.shareIn(viewModelScope, SharingStarted.Eagerly)
sharedFlow
.onEach { flow1Value ->
// handle value from flow1
}.launchIn(viewModelScope)
sharedFlow
.map { value -> value.id}
.distcintUntilChanged()
.flatMapLatest { id ->
flow2(id)
}.onEach { value ->
// handle value
}.launchIn(viewModelScope)
sharedFlow
.map { value -> value.id}
.distcintUntilChanged()
.flatMapLatest { id ->
flow3(id)
}.onEach { value ->
// handle value
}.launchIn(viewModelScope)
Colton Idle
06/05/2022, 11:34 PM