【Koltin Flow(一)】五种创建flow的方式
【Koltin Flow(二)】Flow操作符之末端操作符
【Koltin Flow(三)】Flow操作符之中间操作符(一)
【Koltin Flow(三)】Flow操作符之中间操作符(二)
【Koltin Flow(三)】Flow操作符之中间操作符(三)
val flow1 = listOf(1,2).asFlow()
val flow2 = listOf("one","two","three").asFlow()
flow1.zip(flow2){value1,value2->
"zip $value1,$value2"
}.collect {
Log.d(TAG.TAG,it)
}
2022-08-01 11:29:49.937 4576-4606/edu.test.demo D/Test-TAG: zip 1,one
2022-08-01 11:29:49.937 4576-4606/edu.test.demo D/Test-TAG: zip 2,two
val flow1 = listOf(1,2).asFlow()
val flow2 = listOf("one","two","three").asFlow()
merge(flow1,flow2).collect {value->
Log.d(TAG.TAG,value.toString())
}
2022-08-01 11:33:14.765 4636-4662/edu.test.demo D/Test-TAG: 1
2022-08-01 11:33:14.765 4636-4662/edu.test.demo D/Test-TAG: 2
2022-08-01 11:33:14.765 4636-4662/edu.test.demo D/Test-TAG: one
2022-08-01 11:33:14.765 4636-4662/edu.test.demo D/Test-TAG: two
2022-08-01 11:33:14.765 4636-4662/edu.test.demo D/Test-TAG: three
val flow1 = flow {
repeat(3){
delay(50)
emit(it)
}
}
val flow2 = flow<String> {
delay(100)
emit("one")
delay(100)
emit("two")
}
flow1.combine(flow2){value1,value2->
"combine $value1,$value2"
}.collect {
Log.d(TAG.TAG,it)
}
flow1.combineTransform(flow2){value1,value2->
emit("combineTransform $value1,$value2")
}.collect {
Log.d(TAG.TAG,it)
}
2022-08-01 11:47:13.049 5525-5552/edu.test.demo D/Test-TAG: combine 0,one
2022-08-01 11:47:13.063 5525-5553/edu.test.demo D/Test-TAG: combine 1,one
2022-08-01 11:47:13.114 5525-5552/edu.test.demo D/Test-TAG: combine 2,one
2022-08-01 11:47:13.152 5525-5553/edu.test.demo D/Test-TAG: combine 2,two
2022-08-01 11:47:13.256 5525-5551/edu.test.demo D/Test-TAG: combineTransform 0,one
2022-08-01 11:47:13.258 5525-5551/edu.test.demo D/Test-TAG: combineTransform 1,one
2022-08-01 11:47:13.310 5525-5551/edu.test.demo D/Test-TAG: combineTransform 2,one
2022-08-01 11:47:13.357 5525-5551/edu.test.demo D/Test-TAG: combineTransform 2,two
val flow1 = (1..5).asFlow()
val flow2 = flowOf("一","二","三","四","五")
flowOf(flow1,flow2).flattenConcat().collect {
Log.d(TAG.TAG,"flattenConcat $it")
}
flowOf(flow1,flow2).flattenMerge(2).collect {
Log.d(TAG.TAG,"flattenMerge $it")
}
2022-08-01 13:44:53.938 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 1
2022-08-01 13:44:53.938 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 2
2022-08-01 13:44:53.938 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 3
2022-08-01 13:44:53.938 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 4
2022-08-01 13:44:53.939 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 5
2022-08-01 13:44:53.939 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 一
2022-08-01 13:44:53.939 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 二
2022-08-01 13:44:53.939 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 三
2022-08-01 13:44:53.939 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 四
2022-08-01 13:44:53.939 6409-6435/edu.test.demo D/Test-TAG: flattenConcat 五
2022-08-01 13:44:53.981 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 1
2022-08-01 13:44:53.981 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 一
2022-08-01 13:44:53.981 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 二
2022-08-01 13:44:53.982 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 三
2022-08-01 13:44:53.982 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 四
2022-08-01 13:44:53.982 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 五
2022-08-01 13:44:53.982 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 2
2022-08-01 13:44:53.982 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 3
2022-08-01 13:44:53.982 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 4
2022-08-01 13:44:53.982 6409-6435/edu.test.demo D/Test-TAG: flattenMerge 5
(1..5).asFlow().flatMapConcat {
flowOf(it)
}.collect {
Log.d(TAG.TAG, "flatMapConcat $it")
}
(1..5).asFlow().flatMapMerge(5) {
flowOf(it)
}.collect {
Log.d(TAG.TAG, "flatMapMerge $it")
}
(1..5).asFlow().flatMapLatest {
delay(100)
flowOf(it)
}.collect {
Log.d(TAG.TAG, "flatMapLatest $it")
}
2022-08-01 14:00:36.995 7281-7307/edu.test.demo D/Test-TAG: flatMapConcat 1
2022-08-01 14:00:36.995 7281-7307/edu.test.demo D/Test-TAG: flatMapConcat 2
2022-08-01 14:00:36.995 7281-7307/edu.test.demo D/Test-TAG: flatMapConcat 3
2022-08-01 14:00:36.995 7281-7307/edu.test.demo D/Test-TAG: flatMapConcat 4
2022-08-01 14:00:36.995 7281-7307/edu.test.demo D/Test-TAG: flatMapConcat 5
2022-08-01 14:00:37.036 7281-7307/edu.test.demo D/Test-TAG: flatMapMerge 1
2022-08-01 14:00:37.036 7281-7307/edu.test.demo D/Test-TAG: flatMapMerge 2
2022-08-01 14:00:37.036 7281-7307/edu.test.demo D/Test-TAG: flatMapMerge 4
2022-08-01 14:00:37.036 7281-7307/edu.test.demo D/Test-TAG: flatMapMerge 5
2022-08-01 14:00:37.038 7281-7307/edu.test.demo D/Test-TAG: flatMapMerge 3
2022-08-01 14:00:37.161 7281-7307/edu.test.demo D/Test-TAG: flatMapLatest 5
(1..5).asFlow().onStart {
Log.d(TAG.TAG,"onStart")
}.onCompletion {
Log.d(TAG.TAG,"onCompletion")
}.collect {
Log.d(TAG.TAG,"collect is $it")
}
2022-08-01 14:11:46.202 7384-7411/edu.test.demo D/Test-TAG: onStart
2022-08-01 14:11:46.203 7384-7411/edu.test.demo D/Test-TAG: collect is 1
2022-08-01 14:11:46.203 7384-7411/edu.test.demo D/Test-TAG: collect is 2
2022-08-01 14:11:46.203 7384-7411/edu.test.demo D/Test-TAG: collect is 3
2022-08-01 14:11:46.203 7384-7411/edu.test.demo D/Test-TAG: collect is 4
2022-08-01 14:11:46.204 7384-7411/edu.test.demo D/Test-TAG: collect is 5
2022-08-01 14:11:46.204 7384-7411/edu.test.demo D/Test-TAG: onCompletion
flow {
emit(1)
throw IllegalArgumentException("exception throw")
emit(2)
}.catch {
emit(-1)
Log.e(TAG.TAG,"exception is $it")
}.collect {
Log.d(TAG.TAG,"collect is $it")
}
2022-08-01 14:17:20.843 7465-7491/edu.test.demo D/Test-TAG: collect is 1
2022-08-01 14:17:20.844 7465-7491/edu.test.demo D/Test-TAG: collect is -1
2022-08-01 14:17:20.844 7465-7491/edu.test.demo E/Test-TAG: exception is java.lang.IllegalArgumentException: exception throw
flow<Int> {
}.onEmpty {
Log.d(TAG.TAG, "onEmpty")
}.collect {
Log.d(TAG.TAG, "collect is $it")
}
2022-08-01 14:22:01.544 7598-7625/edu.test.demo D/Test-TAG: onEmpty
flow<Int> {
emit(1)
emit(2)
emit(3)
}.onEach {
Log.d(TAG.TAG, "onEach $it")
}.collect {
Log.d(TAG.TAG, "collect is $it")
}
2022-08-01 14:26:11.313 8010-8035/edu.test.demo D/Test-TAG: onEach 1
2022-08-01 14:26:11.313 8010-8035/edu.test.demo D/Test-TAG: collect is 1
2022-08-01 14:26:11.313 8010-8035/edu.test.demo D/Test-TAG: onEach 2
2022-08-01 14:26:11.313 8010-8035/edu.test.demo D/Test-TAG: collect is 2
2022-08-01 14:26:11.313 8010-8035/edu.test.demo D/Test-TAG: onEach 3
2022-08-01 14:26:11.314 8010-8035/edu.test.demo D/Test-TAG: collect is 3