当前位置:网站首页>[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.
边栏推荐
猜你喜欢

视野 | KeyDB:为 Web 应用而生的高性能 Redis 分支

从想当亿万富翁到职场、创业、爱情、抑郁、学医学武,我的程序人生

4、nerf(pytorch)

Programmers make money and practice, teach you how to do paid courses, self-media, paid articles and paid technical courses to make money

最新版MySQL 8.0 的下载与安装(详细教程)

Participate in open source, let programmers regain their blood and passion

golang八股文整理(持续搬运)

丑陋的程序员

力扣1047-删除字符串中的所有相邻重复项——栈

cookie和session区别
随机推荐
《后浪》程序员版,献给新一代程序员的演讲,何冰《后浪》演讲模仿秀
I went to meet some successful people worth tens of millions on May 1st, and I have some new ideas and inspirations
mysql 中 in 的用法
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
剑指offer(刷题篇12)
机器学习—梯度下降Gradient Descent Optimization—c语言实现
如何使用FirewallD限制网络访问
2022 Pengcheng Cup web
Kyligence 亮相第五届南方信息大会并获评“CIO 优选数字化服务商”
How can I make (a == 1 && a == 2 && a == 3) to be true?
行业案例|数字化经营底座助力寿险行业转型
MySQL kills 10 questions, how many questions can you stick to?
The difference between asyncawait and promise
SOA(面向服务架构)是什么?
【Koltin Flow(二)】Flow操作符之末端操作符
el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能
解决phpstudy无法启动MySQL服务
一个老程序员的2020年总结回顾,2021年如何变的更牛逼
Within the SQL connection table (link connections, left or right, cross connection, full outer join)
Participate in open source, let programmers regain their blood and passion