[Android] 코루틴 Under the hood 이번시간에는 코루틴이 내부적으로 어떻게 동작하는지 확인해보겠습니다 [Continuation Passing Style] CPS == Callbacks //Kotlin suspend fun createPost(token : Token, item : Item) : Post { _ } // Java/JVM Object createPost(Token token, Item item, Continuation cont) { _ } 작성한 코루틴이 컴파일 될때 내부적으로 JVM에서 Byte 코드로 변환되면서 Continuation이 하나가 더 생기게 됩니다. (CPS) [Labels] //Kotlin suspend fun postItem(item : Item) { // LABLE 0 val token = requestT..
[Android] 코루틴 Coroutine Context and Dispatchers 이번에는 coroutine context와 dispatchers 에 대해 알아보겠습니다 [Dispatchers and threads] Coroutine 은 CoroutineContext 에 의해 실행됩니다 CoroutineContext 의 요소에 여러 요소를 설정할수 있는데 요소들 중에는 Job, Dispatchers 등이 있습니다 Dispatchers 는 Coroutine이 어떤 thread 나 thread pool 에서 실행될지를 결정하는 요소입니다 모든 Coroutine builder 는 옵셔널로 CoroutineContext 를 파라미터로 받습니다. ex) launch, async 이것을 통해 dispatcher를 지정할 수 있습니다 fun main() = runBlocking { launch {..
[Android] 코루틴 Composing Suspending Functions 1. 코루틴 왜 써야하는가 2. 코루틴 기초 3. 코루틴 Cancellation and Timeouts 이번에는 suspending functions을 조합하여 코루틴을 유용하게 사용 하는 법에 대해 알아보겠습니다. [Sequential by default] fun main() = runBlocking { val time = measureTimeMillis { val one = doSomethingUsefulOne() val two = doSomethingUsefulTwo() println("The answer is ${one + two}") } println("Completed in $time ms") } suspend fun doSomethingUsefulOne(): Int { delay(1000L)..