一、flow ,emit,onCompletion,collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.runBlocking
fun main(args: Array) {
runBlocking {
flow {
emit("fly")
}.onCompletion {
println("完成")
}.collect {
println(it)
}
}
}
fly
完成
二、函数作为flow
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.runBlocking
fun main(args: Array) {
runBlocking {
load().onCompletion {
println("onCompletion")
}.collect {
println("collect $it")
}
}
}
fun load() = flow {
for (i in 1..3) {
emit(i)
delay(100)
}
}
collect 1
collect 2
collect 3
onCompletion
三、filter
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.runBlocking
fun main(args: Array) {
runBlocking {
load().filter {
it > 3
}.onCompletion {
println("onCompletion")
}.collect {
println("collect $it")
}
}
}
fun load() = flow {
for (i in 1..5) {
emit(i)
delay(100)
}
}
collect 4
collect 5
onCompletion
四、map,重组改写数据。
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.runBlocking
fun main(args: Array) {
runBlocking {
load().filter {
it > 3
}.map {
"fly-$it"
}.onCompletion {
println("onCompletion")
}.collect {
println("collect $it")
}
}
}
fun load() = flow {
for (i in 1..5) {
emit(i)
delay(100)
}
}
collect fly-4
collect fly-5
onCompletion
五、flowOn
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
fun main(args: Array) {
runBlocking {
load().flowOn(Dispatchers.IO)
.onCompletion {
println("onCompletion")
}.collect {
println("collect $it")
}
}
}
fun load() = flow {
for (i in 1..5) {
emit(i)
delay(100)
}
}
collect 1
collect 2
collect 3
collect 4
collect 5
onCompletion
六、zip
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
fun main(args: Array) {
runBlocking {
val flow1 = flowOf(1, 2, 3).onEach {
delay(100)
}
val flow2 = flowOf("a", "b", "c").onEach {
delay(200)
}
flow1.zip(flow2) { i: Int, s: String ->
"$i-$s"
}.onCompletion {
println("onCompletion")
}.collect {
println(it)
}
}
}
1-a
2-b
3-c
onCompletion
七、combine
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
fun main(args: Array) {
runBlocking {
val flow1 = flowOf(1, 2, 3).onEach {
delay(200)
}
val flow2 = flowOf("a", "b", "c").onEach {
delay(100)
}
flow1.combine(flow2) { i: Int, s: String ->
"$i-$s"
}.collect {
println(it)
}
}
}
1-a
1-b
1-c
2-c
3-c
八、conflate 合并
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import kotlin.system.measureTimeMillis
fun main(args: Array) {
runBlocking {
val time = measureTimeMillis {
val f = flow {
for (i in 1..10) {
delay(100)
emit(i)
}
}
val result = f.conflate().onEach { delay(501) }
println(result.toList())
}
println(time)
}
}
[1, 5, 10]
1686
九、debounce去重
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
fun main(args: Array) {
runBlocking {
flow {
emit(1)
delay(50)
emit(2)
delay(40)
emit(3)
delay(300)
emit(4)
delay(30)
emit(5)
delay(700)
}.debounce(300).collect{
print(it)
}
}
}
35