[英]How to send Kotlin Flow events into existing Flow stream via combine?
I have two flows.我有两个流程。
flow1()
emits a stream of integers every second. flow1()
发出一个整数的 stream 。
flow2()
emits an integer when a button is clicked.当单击按钮时,
flow2()
会发出 integer 。
Both flows are combined with combinedFlows()
and the combined results are collected and ouput in the textView
.两个流都与 combineFlows
combinedFlows()
组合,组合结果在textView
中收集和输出。
Problem: The textview is not updating the flow2 click count when I click on the button
.问题:当我点击
button
时,textview 没有更新 flow2 点击计数。 For some reason the combine{ }
operator is not collecting flow2()
on click.由于某种原因,
combine{ }
运算符在点击时未收集flow2()
。 How can I emit flow2()
into the existing flow stream and collect the results in the text view?如何将
flow2()
到现有流 stream 并在文本视图中收集结果?
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>
flow {}
builder is a cold flow, meaning that it executes when a terminal operator is called. flow {}
builder 是一个冷流,这意味着它在调用终端操作符时执行。 When flow {}
builder is invoked it creates a new instance of Flow
.当
flow {}
构建器被调用时,它会创建一个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
.因此,在
combine(flow1(), flow2()) { a, b -> Pair(a, b) }
和flow2().collect()
中调用flow2()
两次将创建两个不同的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: SharedFlow
和StateFlow
是热流,可以作为用户用于以下目的:
private val counter = MutableStateFlow(0)
binding.button.setOnClickListener {
counter.update { count -> count + 1 }
}
fun combineFlows() = combine(flow1(), counter) { a, b -> Pair(a, b) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.