当前位置:网站首页>[Koltin Flow (1)] Five ways to create flow
[Koltin Flow (1)] Five ways to create flow
2022-07-30 05:59:00 【MakerGaoGao】
前言
- flow is the asynchronous version of the sequence,This is a collection type,The values in it are generated one by one.与序列一样,Only when a certain value is required,flow This value is generated on demand,而且 flow Can contain an unlimited number of values.
- Simply put, it is streaming,用过Rxwill be easier to understand.
- flow通过apiand coroutine support,It is more convenient to deal with reactive programming.
创建方式
创建方式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
总结
本篇主要介绍flowThe basic creation part of ,Five ways are introduced,因为内容比较简单,Introduction is less,It is also easier to understand directly on the code.
边栏推荐
- JVM之GC 调优工具 Arthas 实战使用(二)
- [Mysql] CONVERT函数
- 应用实践 | Apache Doris 在百度智能云计费账单系统的应用实践
- Concurrent Programming Review
- 号称年薪30万占比最多的专业,你知道是啥嘛?
- G巴士计数(Google Kickstart2014 Round D Problem B)(DAY 89)
- Within the SQL connection table (link connections, left or right, cross connection, full outer join)
- 2022年比若依更香的开源项目
- [GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
- idea设置自动带参数的方法注释(有效)
猜你喜欢
随机推荐
cnpm安装步骤
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
应用实践 | Apache Doris 在百度智能云计费账单系统的应用实践
el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能
从字节码角度带你彻底理解异常中catch,return和finally,再也不用死记硬背了
mysql基础(4)
Mysql8.+学习笔记
力扣1047-删除字符串中的所有相邻重复项——栈
Programmers make money and practice, teach you how to do paid courses, self-media, paid articles and paid technical courses to make money
图形镜像对称(示意图)
Participate in open source, let programmers regain their blood and passion
SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
Kyligence 再获 CRN, insideBIGDATA 两大国际奖项认可
G巴士计数(Google Kickstart2014 Round D Problem B)(DAY 89)
MySQL索引从入门到深入学习
MySQL(3)
RadonDB MySQL Kubernetes 2.2.0 发布!
倒计数(来源:Google Kickstart2020 Round C Problem A)(DAY 88)
PyCharm usage tutorial (more detailed, picture + text)
JVM之GC 调优工具 Arthas 实战使用(二)