当前位置:网站首页>[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.
边栏推荐
- 使用DataEase开源工具制作一个高质量的数据大屏
- MySQL夺命10问,你能坚持到第几问?
- mysql 中 in 的用法
- 开源之夏 2022 与您相约!
- go版本升级
- idea 编译protobuf 文件的设置使用
- How MySQL to prepare SQL pretreatment (solve the query IN SQL pretreatment can only query out the problem of a record)
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
- 最新版MySQL 8.0 的下载与安装(详细教程)
- Path dependence: the poor hard science to counter attack breakthrough
猜你喜欢
随机推荐
剑指offer(刷题篇12)
curl (7) Failed connect to localhost8080; Connection refused
路径依赖:穷人很难逆袭突破的科学道理
是时候不得不学英语了,技多不压身,给自己多条路
[GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
4、nerf(pytorch)
黄金圈法则:成功者必备的深度思考方法
微信支付及支付回调
Path dependence: the poor hard science to counter attack breakthrough
程序员大保健指南,给自己的身心偶尔放松的机会
无代码开发平台子管理员入门教程
[GStreamer] 插件的名字要和GST_PLUGIN_DEFINE匹配
mysql基础(4)
参与开源,让程序员找回热血和激情
MySQL夺命10问,你能坚持到第几问?
idea 编译protobuf 文件的设置使用
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
从驱动表和被驱动表来快速理解MySQL中的内连接和外连接
mysql basics (4)
坠落的蚂蚁(北京大学考研机试题)









