当前位置:网站首页>Create a flow using flow builder in kotlin
Create a flow using flow builder in kotlin
2022-07-23 17:36:00 【Xiao Chen knocked the code randomly】
Preface
In this article , We will discuss different types of flow builders and how to use them to create flows .
Let's get started .
We will introduce the following :
- Type of flow generator
- Use Flow Builder Create stream
Type of flow generator
Yes 4 There are three types of traffic generators :
- flowOf(): It is used to create flows from a given set of projects .
- asFlow(): It's an extension function , It helps to convert types to streams .
- flow{}: This is where we are Flow Of Hello World The... Used in the example .
- channelFlow{}: This builder uses the send provided by the builder itself to create a stream with elements .
Example :
lowOf()
flowOf(4, 2, 5, 1, 7)
.collect {
Log.d(TAG, it.toString())
}
asFlow()
(1..5).asFlow()
.collect {
Log.d(TAG, it.toString())
}
flow{}
flow {
(0..10).forEach {
emit(it)
}
}
.collect {
Log.d(TAG, it.toString())
}
channelFlow{}
channelFlow {
(0..10).forEach {
send(it)
}
}
.collect {
Log.d(TAG, it.toString())
}
Now? , We will learn how to use Flow Builder creation Flow. We can use Flow Builder Create for any task Flow.
Use Flow Builder Create stream
Let's learn it by example .
Move files from one location to another
ad locum , We will use Flow Builder establish Flow, In order to move files from one location to another in the background thread , And send the completion status on the main thread .
val moveFileflow = flow {
// move file on background thread
FileUtils.move(source, destination)
emit("Done")
}
.flowOn(Dispatcher.Default)
CoroutineScope(Dispatchers.Main).launch {
moveFileflow.collect {
// when it is done
}
}
Download Image
ad locum , We will use Flow Builder Create our Flow To download the image , This will download the image in the background thread and continue to send the progress to the collector on the main thread .
val downloadImageflow = flow {
// our image downloading code
// start downloading
// send progress
emit(10)
// downloading...
// ......
// ......
// send progress
emit(75)
// downloading...
// ......
// ......
// send progress
emit(100)
}
.flowOn(Dispatcher.Default)
CoroutineScope(Dispatchers.Main).launch {
downloadImageflow.collect {
// we will get the progress here
}
}
This is the creation of Flow The way .
边栏推荐
- How many common SQL misuses are there in MySQL?
- Encapsulate the general connection and query of the project with pymysql
- 可视化机房管理
- 你真的了解Redis的持久化机制吗?
- quota命令详细拓展使用方法,RHEL 7中quota命令搭载方法!磁盘容量配额!
- 爱可可AI前沿推介(7.23)
- [operation] Yan Yi (Internet new technology operation)
- 小程序商城如何精细化运营?
- sns_ sensor_ instance_ api
- 职场3道坎:年薪30万、50万、100万
猜你喜欢
随机推荐
Differences between nvisual generic cabling management software and network management software
封玩家IP和机器码以及解开被封的教程
Software configuration | Anaconda download, installation, environment configuration and uninstall
LQR 控制学习-LQR控制 MATLAB官方教程-LQR 控制器_状态空间系统Matlab/Simulink建模分析
Food safety | ham sausage lunch meat, is it really so unbearable?
Opencv finding the intersection of two regions
不掌握这些坑,你敢用BigDecimal吗?
记录一下MySql update会锁定哪些范围的数据
Single cell thesis record (part19) -- a comprehensive comparison on cell type composition information for St data
“如今,代码数已膨胀至天文级别”
线程池,我是谁?我在哪儿?
[JS] check whether the date object is invalid date
How many common SQL misuses are there in MySQL?
[MySQL Cluster fault recovery]
Repository XXX does not have a Realease file「建议收藏」
Food safety | attention to smoking food, do you know this knowledge
单细胞文献学习(part6)--ForestFireClustering for sc sequencing combines iterative label propagation with ...
Log slimming operation: from 5g to 1g!
【redis入门系列】redis的数据类型及相关命令
Single cell literature learning (part6) -- forestfireclustering for SC sequencing combinations iterative label promotion with








