当前位置:网站首页>[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.
边栏推荐
- RadonDB MySQL on K8s 2.1.4 发布!
- Programmers make money and practice, teach you how to do paid courses, self-media, paid articles and paid technical courses to make money
- 丑陋的程序员
- Golang——从入门到放弃
- It's time to have to learn English, give yourself multiple paths
- mysql高阶语句(一)
- RadonDB MySQL on K8s 2.1.3 发布!
- cmd(命令行)操作或连接mysql数据库,以及创建数据库与表
- [GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
- 倒计数(来源:Google Kickstart2020 Round C Problem A)(DAY 88)
猜你喜欢
随机推荐
[Mysql] DATEDIFF function
ugly programmer
无代码开发平台子管理员入门教程
黄金圈法则:成功者必备的深度思考方法
go版本升级
leetcode刷题
2022年SQL大厂高频实战面试题(详细解析)
cnpm安装步骤
MySQL kills 10 questions, how many questions can you stick to?
mysql基础(4)
839. 模拟堆
[GLib] 什么是GType
应用实践 | Apache Doris 在百度智能云计费账单系统的应用实践
JVM之GC 调优基础知识(一)
IDEA的database使用教程(使用mysql数据库)
4、nerf(pytorch)
[GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
The use of Conluce, an online document management system
【Koltin Flow(二)】Flow操作符之末端操作符
从驱动表和被驱动表来快速理解MySQL中的内连接和外连接