LoginSignup
0
0

【Kotlin】Flow から条件を満たす値が流れてくるまで suspend する

Posted at

次のようにすると、
0〜9が流れてきている間は collect が suspend し続け、
10 が流れてきたら collect が終了して "Finshed" が出力される。

flow {
    var count = 0
    while (true) {
        emit(count++)
    }
}.takeWhile {
    it < 10
}.collect {
    println(it)
}
println("Finished")

/以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0