๊นจ์•Œ ๊ฐœ๋… ๐Ÿ“‘/Kotlin 25

[Kotlin] Coroutines Flow - Flow.combine()๊ณผ Flow.stateIn()

#1 ๊ฐœ์š” combineReturns a Flow whose values are generated with transform function by combining the most recently emitted values by each flow. It can be demonstrated with the following example: val flow = flowOf(1, 2).onEach { delay(10) }val flow2 = flowOf("a", "b", "c").okotlinlang.org stateInConverts a cold Flow into a hot StateFlow that is started in the given coroutine scope, sharing the most re..

[Kotlin] Coroutines Flow - StateFlow

#1 ๊ฐœ์š” StateFlowA SharedFlow that represents a read-only state with a single updatable data value that emits updates to the value to its collectors. A state flow is a hot flow because its active instance exists independently of the presence of collectors. Its current valukotlinlang.orgSharedFlow์˜ ์ผ์ข…์ธ StateFlow์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค. #2 SharedFlow์™€์˜ ๋น„๊ต์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ, StateFlow๋Š” ๊ธฐ์กด SharedFlow์— State์˜ ํŠน์„ฑ์„ ๊ฒฐํ•ฉํ•œ Flow๋‹ค..

[Kotlin] Coroutines Flow - Intermediate operator

#1 ๊ฐœ์š”#1-1 ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž (Intermediate operator) Asynchronous Flow | Kotlin kotlinlang.org์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค. #1-2 ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž์˜ ๊ตฌ์กฐ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž๋Š” ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด๋ฆ„์— ์žˆ๋Š” '์ค‘๊ฐ„'์€, emit๊ณผ collect ์‚ฌ์ด '์ค‘๊ฐ„'์„ ์˜๋ฏธํ•œ๋‹ค. ์‹œ์ž‘์ ์ด emit์ด๊ณ  ๋„์ฐฉ์ ์ด collect์ธ ๋ฐฐ์ˆ˜๊ด€ ์† ๋ฌผ์˜ ํ๋ฆ„์„ intermediate operator๊ฐ€ ํ•˜์ด์žฌํ‚นํ•˜๋Š” ๋А๋‚Œ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€, ์•ž์œผ๋กœ(=๋ฏธ๋ž˜์—) emit๋  ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•ํ•˜๋Š” ์—ฐ์‚ฐ์ž๋Š” ๊ฒฐ์ฝ” ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฏธ(=๊ณผ๊ฑฐ์—) emit๋œ ๋ฐ์ดํ„ฐ์— ํŠน์ • ์—ฐ์‚ฐ์„ ๊ฐ€ํ•˜์—ฌ, collect์— ์ „๋‹ฌํ•˜๋Š” ์—ฐ์‚ฐ์ž๋‹ค. ์ค‘๊ฐ„ ์—ฐ์‚ฐ์ž์ด๋‹ˆ ๋ง์ด๋‹ค. #2 Flow์— ๊ฐ€์šฉ..

[Kotlin] Coroutines Flow - Back pressure์™€ ๊ทธ ์ฒ˜๋ฆฌ

#1 ๊ฐœ์š”Coroutines Flow๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ํ˜„์ƒ์ธ ๋ฐฑ ํ”„๋ ˆ์…”(Back pressure)์— ๋Œ€ํ•ด ์‚ดํŽด๋ณธ๋‹ค. ๋˜, ๋ฐฑ ํ”„๋ ˆ์…”๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์‚ดํŽด๋ณธ๋‹ค. ์ด ๋•Œ, ๋ฐฑ ํ”„๋ ˆ์…”๋Š” ์—๋Ÿฌ๊ฐ€ ์•„๋‹Œ ์ž์—ฐ์Šค๋Ÿฌ์šด ํ˜„์ƒ์ผ ๋ฟ์ด๋‹ค. ๋”ฐ๋ผ์„œ, ๋ฐฑ ํ”„๋ ˆ์…”๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค๋Š” ํ‘œํ˜„์€ ์ •ํ™•ํ•˜์ง€ ์•Š๋‹ค. ๋ฐฑ ํ”„๋ ˆ์…”์— ๋Œ€์ฒ˜ํ•œ๋‹ค๋Š” ํ‘œํ˜„์ด ์˜ณ๋‹ค. #2 ๋ฐฑ ํ”„๋ ˆ์…”#2-1 ๋ฐฑ ํ”„๋ ˆ์…”๊ฐ€ ์—†๋Š” ์ฝ”๋“œimport kotlinx.coroutines.*import kotlinx.coroutines.flow.Flowimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.flow// 1์ดˆ๋งˆ๋‹ค 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” countfun startCountUp(count: Mu..

[Kotlin] Coroutines Flow - ๊ธฐ์ดˆ

#1 Coroutines Flow#1-1 ๊ฐœ์š” FlowFlow An asynchronous data stream that sequentially emits values and completes normally or with an exception. Intermediate operators on the flow such as map, filter, take, zip, etc are functions that are applied to the upstream flow or flows and return a dokotlinlang.orgFlow๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ Coroutine์„ ์‚ฌ์šฉํ•ด ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆผ์„ ์ฒ˜๋ฆฌํ•˜๋Š” API๋‹ค. ์ด๋ฅผ ๋ฐ˜์‘ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค. ๋ฐ˜์‘ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•œ ๋งˆ๋””๋กœ ์ •์˜ํ•˜๋ฉด, ..

[Kotlin] Coroutines - ํ•œ Scope ๋‚ด์—์„œ์˜ ๊ณ„์ธต ๊ด€๊ณ„

#1 ์ด์ „ ๊ธ€ [Kotlin] Coroutines - Coroutine builder#1 Coroutine builder kotlinx-coroutines-coreCore primitives to work with coroutines. Coroutine builder functions: Coroutine dispatchers implementing CoroutineDispatcher: More context elements: Synchronization primitives for coroutines: Top-level suspendinkenel.tistory.com์œ„ ๊ฒŒ์‹œ๊ธ€์˜ CoroutineScope์˜ ์ƒ๋žต์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ #6-4์—์„œ ์ด์–ด์ง€๋Š” ๊ธ€์ด๋‹ค. ์ด์ „ ๊ธ€์—์„  CoroutineScope์„ ์ƒ๋žตํ•˜๋Š” ๊ฒŒ..

[Kotlin] ์œ„์ž„ ํ”„๋กœํผํ‹ฐ (Delegated properties)

#1 Delegated properties#1-1 ๊ฐœ์š” Delegated properties | Kotlin kotlinlang.org์œ„์ž„ ํ”„๋กœํผํ‹ฐ๋Š”, getter์™€ setter ๋กœ์ง์„ ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ์œ„์ž„(delegate)ํ•˜๋Š” ์ฝ”ํ‹€๋ฆฐ ํ”„๋กœํผํ‹ฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋ณธ ๊ฒŒ์‹œ๊ธ€์—์„œ๋Š” ์œ„์ž„ ํ”„๋กœํผํ‹ฐ์˜ ๊ธฐ์ œ์— ๋Œ€ํ•ด ํƒ๊ตฌํ•˜๊ณ  ๋˜ ์œ„์ž„ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ํ•ด๋ณธ๋‹ค. #1-2 ์œ„์ž„(delegate)์˜ ์˜๋ฏธ [Kotlin] ํ”„๋กœํผํ‹ฐ(Property)#1 ํ•„๋“œ์™€ ํ”„๋กœํผํ‹ฐ #1-1 ํ”„๋กœํผํ‹ฐ์˜ ๊ฐœ๋… // Java String name = "steve"; // Field System.out.println("my name is" + name); name = "kevin"; --- // Kotlin var name : String = "..

[Kotlin] Coroutines - ViewModelScope

๋ณธ ๊ฒŒ์‹œ๊ธ€์˜ Coroutine ๊ฐœ๋…์€ Android ๋‚ด์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ์ „์ œ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค. #1 ViewModel ์† ์ „ํ†ต์ ์ธ ๋ฐฉ์‹์˜ Coroutinesimport androidx.lifecycle.ViewModelimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.Jobimport kotlinx.coroutines.launchclass SampleViewModel : ViewModel() {    private val myJob = Job()    private val myScope = CoroutineScope(Dispatchers.IO + myJob)    fun s..

[Kotlin] Coroutines - ์Šค๋ ˆ๋“œ ์ „ํ™˜

๋ณธ ๊ฒŒ์‹œ๊ธ€์˜ Coroutine ๊ฐœ๋…์€ Android ๋‚ด์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ์ „์ œ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค. #1 Background Thread์˜ ํ•œ๊ณ„ - UI ์กฐ์ž‘ ๋ถˆ๊ฐ€๋Šฅ...class MainActivity : AppCompatActivity() {    ...    override fun onCreate(savedInstanceState: Bundle?) {        ...        btnDwCoroutine.setOnClickListener {            CoroutineScope(Dispatchers.IO).launch {                downloadData()            }        }    }    private fun downloadData() {        ..

[Kotlin] Coroutines - Parallel Decomposition

#1 ์ž‘์—…์˜ ์ฒ˜๋ฆฌ ๋ฐฉ์‹#1-1 ๊ฐ€์ •์œ„์™€ ๊ฐ™์€ ์ด 7๊ฐœ์˜ ์ž‘์—…์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๊ฐ ์ž‘์—…์ด ์†Œ์š” ์‹œ๊ฐ„์€ 5, 10, 15, 10, 20, 25, 5๋‹ค. #1-2 ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์€ ์„ ํ˜•์ (Serial)์œผ๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. #1-3 ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์˜ ๊ตฌํ˜„import kotlinx.coroutines.*data class Food(val name: String, val cookingTimeInSeconds: Long)suspend fun cook(food: Food) { delay(1000 * food.cookingTimeInSeconds) println("${food.name} ์™„๋ฃŒ")}fun main() { val foods = arrayOf( Food("๊ตญ๋ฐฅ", 5..