目录
【Koltin Flow(一)】五种创建flow的方式
【Koltin Flow(二)】Flow操作符之末端操作符
【Koltin Flow(三)】Flow操作符之中间操作符(一)
【Koltin Flow(三)】Flow操作符之中间操作符(二)
【Koltin Flow(三)】Flow操作符之中间操作符(三)
【Koltin Flow(四)】Flow背压
【Koltin Flow(五)】SharedFlow及StateFlow
前言
- flow 是序列的异步版本,这是一种收集类型,其中的值是逐个生成的。与序列一样,只有需要某个值时,flow 才会根据需要生成该值,而且 flow 可以包含无限数量的值。
- 简单来说就是流式处理,用过Rx的会比较容易理解。
- flow通过api和协程支持,处理响应式编程更方便。
创建方式
创建方式1 flow{}
代码如下
flow {
repeat(10){
delay(10)
emit(it)
}
}.collect {
Log.d(TAG.TAG,"method 1 it is $it")
}
日志如下:
2022-07-28 17:05:01.070 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 0
2022-07-28 17:05:01.080 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 1
2022-07-28 17:05:01.091 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 2
2022-07-28 17:05:01.106 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 3
2022-07-28 17:05:01.130 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 4
2022-07-28 17:05:01.140 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 5
2022-07-28 17:05:01.153 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 6
2022-07-28 17:05:01.163 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 7
2022-07-28 17:05:01.174 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 8
2022-07-28 17:05:01.186 3699-3724/edu.test.demo D/Test-TAG: method 1 it is 9
创建方式2 flowof
代码如下
flowOf(1,2,3,4,5).collect {
Log.d(TAG.TAG,"method 2 it is $it")
}
日志如下:
2022-07-28 17:05:01.187 3699-3724/edu.test.demo D/Test-TAG: method 2 it is 1
2022-07-28 17:05:01.187 3699-3724/edu.test.demo D/Test-TAG: method 2 it is 2
2022-07-28 17:05:01.187 3699-3724/edu.test.demo D/Test-TAG: method 2 it is 3
2022-07-28 17:05:01.187 3699-3724/edu.test.demo D/Test-TAG: method 2 it is 4
2022-07-28 17:05:01.187 3699-3724/edu.test.demo D/Test-TAG: method 2 it is 5
创建方式3 asFlow
代码如下
listOf(6,7,8,9,10).asFlow().collect {
Log.d(TAG.TAG,"method 3 it is $it")
}
日志如下:
2022-07-28 17:05:01.204 3699-3724/edu.test.demo D/Test-TAG: method 3 it is 6
2022-07-28 17:05:01.204 3699-3724/edu.test.demo D/Test-TAG: method 3 it is 7
2022-07-28 17:05:01.204 3699-3724/edu.test.demo D/Test-TAG: method 3 it is 8
2022-07-28 17:05:01.204 3699-3724/edu.test.demo D/Test-TAG: method 3 it is 9
2022-07-28 17:05:01.204 3699-3724/edu.test.demo D/Test-TAG: method 3 it is 10
创建方式4 channelFlow{}
代码如下
channelFlow {
repeat(10){
delay(10)
send(it)
}
}.collect {
Log.d(TAG.TAG,"method 4 it is $it")
}
日志如下:
2022-07-28 17:05:01.227 3699-3724/edu.test.demo D/Test-TAG: method 4 it is 0
2022-07-28 17:05:01.238 3699-3724/edu.test.demo D/Test-TAG: method 4 it is 1
2022-07-28 17:05:01.250 3699-3724/edu.test.demo D/Test-TAG: method 4 it is 2
2022-07-28 17:05:01.260 3699-3724/edu.test.demo D/Test-TAG: method 4 it is 3
2022-07-28 17:05:01.272 3699-3725/edu.test.demo D/Test-TAG: method 4 it is 4
2022-07-28 17:05:01.284 3699-3725/edu.test.demo D/Test-TAG: method 4 it is 5
2022-07-28 17:05:01.295 3699-3725/edu.test.demo D/Test-TAG: method 4 it is 6
2022-07-28 17:05:01.306 3699-3725/edu.test.demo D/Test-TAG: method 4 it is 7
2022-07-28 17:05:01.317 3699-3725/edu.test.demo D/Test-TAG: method 4 it is 8
2022-07-28 17:05:01.329 3699-3724/edu.test.demo D/Test-TAG: method 4 it is 9
创建方式5 callbackFlow{}
代码如下
callbackFlow<String> {
delay(100)
trySendBlocking("123456")
awaitClose { }
}.collect {
Log.d(TAG.TAG,"method 5 it is $it")
}
日志如下:
2022-07-28 17:05:01.438 3699-3724/edu.test.demo D/Test-TAG: method 5 it is 123456
总结
本篇主要介绍flow的基础创建部分,介绍了五种方式,因为内容比较简单,介绍比较少,直接上代码也比较好理解。