Appearance
Flow 默认是冷流:收集才执行,利于资源节约。
fun ticker(): Flow<Int> = flow { var i = 0 while (true) { emit(i++) delay(1000) } }
stateIn / shareIn 把冷流热化,供多收集器订阅。
stateIn
shareIn
buffer、conflate 处理生产快于消费;Android UI 常配合 repeatOnLifecycle 避免后台收集。
buffer
conflate
repeatOnLifecycle
新项目优先 Flow;维护遗留 Rx 可用 kotlinx-coroutines-rx2 桥接。
kotlinx-coroutines-rx2