当前位置:网站首页>【Koltin Flow(一)】五种创建flow的方式
【Koltin Flow(一)】五种创建flow的方式
2022-07-30 05:29:00 【MakerGaoGao】
前言
- 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的基础创建部分,介绍了五种方式,因为内容比较简单,介绍比较少,直接上代码也比较好理解。
边栏推荐
- 一文带你吃透js处理树状结构数据的增删改查
- MYSQL-InnoDB的线程模型
- RadonDB PostgreSQL on K8s 2.1.0 发布!
- curl (7) Failed connect to localhost8080; Connection refused
- [GLib] 什么是GType
- 关于组织开展2022年广东省技术先进型服务企业认定工作的通知
- go language study notes 4
- leetcode hot 100(刷题篇11)(231/235/237/238/292/557/240/36)offer/3/4/5
- mysql cannot connect remotely Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060 "Unknown error")
- 【Redis高手修炼之路】Jedis——Jedis的基本使用
猜你喜欢
随机推荐
429. N 叉树的层序遍历(两种解法)
MySQL - 函数及约束命令
Concurrent Programming Review
MySQL(3)
1475. 商品折扣后的最终价格
Redis学习
JVM之GC 调优基础知识(一)
2022鹏城杯web
CSDN Meetup 回顾 丨从数据湖到指标中台,提升数据分析 ROI
视野 | KeyDB:为 Web 应用而生的高性能 Redis 分支
oracle触发器的自治事务
罗湖区工匠技能领军人才奖励项目申请指南
程序员赚钱实操,手把手教你做付费课程,自媒体,付费文章及付费技术课赚钱
容器化 | 在 K8s 上部署 RadonDB MySQL Operator 和集群
4、nerf(pytorch)
mysql cannot connect remotely Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060 "Unknown error")
[Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset
容器化|在 S3 备份恢复 RadonDB MySQL 集群数据
SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
JVM 类加载机制 超详细学习笔记(三)









