小编Raj*_*esh的帖子

发生特定情况时如何停止 Kotlin 流程

如果代码中发生某些情况,我想取消 kotlin 流程。

假设我有一个方法如下

fun test(): Flow<String> = flow {
    val lst = listOf("A", "B", "C")

    while(true) {
        lst.forEach { emit(it) }

    //If some condition occurs, need to return from here, else continue
    //How to stop flow here
    }
}
Run Code Online (Sandbox Code Playgroud)

并称其为

test().collect { println(it)}
Run Code Online (Sandbox Code Playgroud)

问题是,如何停止流在某些条件下(来自流生成器或流生成器之外)产生任何内容?

asynchronous reactive-programming kotlin rx-java kotlin-flow

3
推荐指数
1
解决办法
4955
查看次数