Android/Coroutine
-
#3 Coroutine 형식Android/Coroutine 2021. 10. 10. 14:31
서론 #2에서 Coroutine 구성요소를 살펴봤다. 이제는 실제 Coroutine이 사용되는 형식을 살펴보겠다. 1. Suspend suspend fun myCoroutine() { delay(1000) println("myCoroutine") } #1에서 잠깐 언급한 suspend다. 위와 같이 어떠한 함수 내부에서 Coroutine(suspend function)을 실행할 수 있게 만들어주려면 suspend 라는 단어를 붙여주어야 한다. suspend를 붙여줌으로써 해당 함수는 하나의 Coroutine으로 동작하기 위한 자격을 얻게되며, 일시중지 및 재개(suspend & resume)이 가능해진다. 이렇게 만들어진 suspend function은 아무데서나 사용될 수 없고 어떠한 Coroutine..
-
#2 Coroutine 구성요소Android/Coroutine 2021. 10. 6. 15:02
서론 #1에서는 Coroutine의 작동 방식과, 장점에 대해 간단히 살펴보았다. 이번 #2에서는 Coroutine의 기본적인 형태와 구성요소에 대해 살펴볼 것이다. Coroutine 만들기 일반적인 Coroutine은 아래와 같은 형태를 갖는다. val scope = CoroutineScope(CoroutineContext) val job = scope.CoroutineBuilder{ //Do Something } 위의 코드는 클래스의 이름을 이용해 나타낸 것이고 실제 코드에서 사용하는 예시는 다음과 같다. val scope = CoroutineScope(Dispatchers.IO) //CoroutineScope 생성 val job = scope.launch{ //아래 코드를 방금 만들어준 Corout..
-
#1. Why CoroutineAndroid/Coroutine 2021. 10. 5. 17:20
서론 안드로이드에서 비동기 백그라운드 작업을 하는 AsyncTask가 @Deprecated가 붙고 이 커밋에서 다음과 같은 코멘트가 남겨졌다. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes. It also has inconsistent behavior on different versions of the platform, swallows exceptions ..