I have two flows.
flow1() emits a stream of integers every second.
flow2() emits an integer when a button is clicked.
Both flows are combined with combinedFlows() and the combined results are collected and ouput in the textView.
Problem: The textview is not updating the flow2 click count when I click on the button. For some reason the combine{ } operator is not collecting flow2() on click. How can I emit flow2() into the existing flow stream and collect the results in the text view?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
CoroutineScope(Dispatchers.Main).launch {
combineFlows().collect {
binding.textView.text = "flow1: ${it.first} and flow2 click count: ${it.second}"
}
}
binding.button.setOnClickListener {
CoroutineScope(Dispatchers.Main).launch {
flow2().collect()
}
}
}
fun flow1() = (1..1000).asFlow().onEach { delay(1000) }
fun flow2() = flow {
var i = 0
i
emit("$i")
}
fun combineFlows() = combine(flow1(), flow2()) { a, b -> Pair(a, b) }
}
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@ id/button"
android:layout_width="170dp"
android:layout_height="35dp"
android:textSize="11sp"
android:text="Emit Flow"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
flow {} builder is a cold flow, meaning that it executes when a terminal operator is called. When flow {} builder is invoked it creates a new instance of Flow. Therefore, calling flow2() two times in combine(flow1(), flow2()) { a, b -> Pair(a, b) } and flow2().collect() will create two different instances of Flow.
Button clicks should be regarded as a hot flow - all collectors don't get all events, they receive only last event (or only a couple of last events depending on how the hot flow is configured).
SharedFlow and StateFlow are hot flows, that can be user for such purpose:
private val counter = MutableStateFlow(0)
binding.button.setOnClickListener {
counter.update { count -> count 1 }
}
fun combineFlows() = combine(flow1(), counter) { a, b -> Pair(a, b) }
