#1 ๊ฐ์
#1-1 ์ค๊ฐ ์ฐ์ฐ์ (Intermediate operator)
Asynchronous Flow | Kotlin
kotlinlang.org
์ค๊ฐ ์ฐ์ฐ์์ ๋ํด ์ดํด๋ณธ๋ค.
#1-2 ์ค๊ฐ ์ฐ์ฐ์์ ๊ตฌ์กฐ
์ค๊ฐ ์ฐ์ฐ์๋ ์ด๋ฆ ๊ทธ๋๋ก์ ์ญํ ์ ์ํํ๋ค. ์ด๋ฆ์ ์๋ '์ค๊ฐ'์, emit๊ณผ collect ์ฌ์ด '์ค๊ฐ'์ ์๋ฏธํ๋ค. ์์์ ์ด emit์ด๊ณ ๋์ฐฉ์ ์ด collect์ธ ๋ฐฐ์๊ด ์ ๋ฌผ์ ํ๋ฆ์ intermediate operator๊ฐ ํ์ด์ฌํนํ๋ ๋๋์ด๋ผ๊ณ ๋ณด๋ฉด ๋๋ค. ์ฌ๊ธฐ์ ์ค์ํ ์ ์, ์์ผ๋ก(=๋ฏธ๋์) emit๋ ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๋ ์ฐ์ฐ์๋ ๊ฒฐ์ฝ ์๋๋ผ๋ ๊ฒ์ด๋ค. ์ด๋ฏธ(=๊ณผ๊ฑฐ์) emit๋ ๋ฐ์ดํฐ์ ํน์ ์ฐ์ฐ์ ๊ฐํ์ฌ, collect์ ์ ๋ฌํ๋ ์ฐ์ฐ์๋ค. ์ค๊ฐ ์ฐ์ฐ์์ด๋ ๋ง์ด๋ค.
#2 Flow์ ๊ฐ์ฉํ ์ค๊ฐ ์ฐ์ฐ์ ๋ชฉ๋ก
#2-1 ๋ฒ ์ด์ค ์ฝ๋
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
fun main() {
// 1, 2, 3, ...์ ๋ฐฉ์ถํ๋ Flow
val numberFlow: Flow<Int> = flow {
for (i in 1..10) {
delay(100)
println("emitted: $i")
emit(i)
}
}
// Flow๋ฅผ collect
runBlocking {
numberFlow.collect { value ->
println("collected: $value")
}
}
}
/* ↑ ↑ ↑ ์ถ๋ ฅ ๊ฒฐ๊ณผ
emitted: 1
collected: 1
emitted: 2
collected: 2
emitted: 3
collected: 3
emitted: 4
collected: 4
emitted: 5
collected: 5
emitted: 6
collected: 6
emitted: 7
collected: 7
emitted: 8
collected: 8
emitted: 9
collected: 9
emitted: 10
collected: 10
*/
๋จผ์ , ์๋ฌด๋ฐ ์ค๊ฐ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ง ์์ ๋ฒ ์ด์ค ์ฝ๋๋ค. ์ด ์ฝ๋๋ฅผ ์ค๊ฐ ์ฐ์ฐ์๋ฅผ ์ด์ฉํ๋ ์ฝ๋๋ก ์์ ํด๋ณด๊ฒ ๋ค.
#2-2 map()
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
fun main() {
// 1, 2, 3, ...์ ๋ฐฉ์ถํ๋ Flow
val numberFlow: Flow<Int> = flow {
for (i in 1..10) {
delay(100)
println("emitted: $i")
emit(i)
}
}
// Flow๋ฅผ collect
runBlocking {
numberFlow
.map { value -> // ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๋ณํ
"Hello ${value}!"
}
.collect { value ->
println("collected: $value")
}
}
}
/* ↑ ↑ ↑ ์ถ๋ ฅ ๊ฒฐ๊ณผ
emitted: 1
collected: Hello 1!
emitted: 2
collected: Hello 2!
emitted: 3
collected: Hello 3!
emitted: 4
collected: Hello 4!
emitted: 5
collected: Hello 5!
emitted: 6
collected: Hello 6!
emitted: 7
collected: Hello 7!
emitted: 8
collected: Hello 8!
emitted: 9
collected: Hello 9!
emitted: 10
collected: Hello 10!
*/
map()์ emit๋ ๋ฐ์ดํฐ๋ฅผ ๋ณํํ์ฌ collect()์ ์ ๋ฌํ๋ค.
#2-3 filter()
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
fun main() {
// 1, 2, 3, ...์ ๋ฐฉ์ถํ๋ Flow
val numberFlow: Flow<Int> = flow {
for (i in 1..10) {
delay(100)
println("emitted: $i")
emit(i)
}
}
// Flow๋ฅผ collect
runBlocking {
numberFlow
.filter { value -> // ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๊ท์น์ ๋ง์ถฐ ๊ฑธ๋ฌ๋
value % 3 == 0
}
.collect { value ->
println("collected: $value")
}
}
}
/* ↑ ↑ ↑ ์ถ๋ ฅ ๊ฒฐ๊ณผ
emitted: 1
emitted: 2
emitted: 3
collected: 3
emitted: 4
emitted: 5
emitted: 6
collected: 6
emitted: 7
emitted: 8
emitted: 9
collected: 9
emitted: 10
*/
filter()๋ boolean ์ฐ์ฐ์ ์ธ์๋ก ๋ฐ๋๋ค. ๊ทธ ์ฐ์ฐ์ด true๋ฉด collect์ ์ ๋ฌํ๊ณ , ์๋๋ผ๋ฉด ์ ๋ฌํ์ง ์๋๋ค.
#2-4 ๊ธฐํ
Asynchronous Flow | Kotlin
kotlinlang.org
take, toList, toSet, first, single, reduce, fold ๋ฑ ๋ค๋ฅธ ์ค๊ฐ ์ฐ์ฐ์๋ค์ ๋ํ ๋ด์ฉ์ ์ ๋งํฌ์์ ํ์ธํ ์ ์๋ค.
#3 ์์ฝ
์ค๊ฐ ์ฐ์ฐ์๋ ํ์ด์ฌํน์ด๋ค.
'๊นจ์ ๊ฐ๋ ๐ > Kotlin' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kotlin] Coroutines Flow - Flow.combine()๊ณผ Flow.stateIn() (0) | 2024.08.29 |
---|---|
[Kotlin] Coroutines Flow - StateFlow (0) | 2024.08.17 |
[Kotlin] Coroutines Flow - Back pressure์ ๊ทธ ์ฒ๋ฆฌ (0) | 2024.08.15 |
[Kotlin] Coroutines Flow - ๊ธฐ์ด (0) | 2024.08.01 |
[Kotlin] Coroutines - ํ Scope ๋ด์์์ ๊ณ์ธต ๊ด๊ณ (0) | 2024.07.31 |