본문 바로가기
[Coroutine] Coroutine scope functions Approaches before coroutine scope functions # GlobalScope 은 EmptyCoroutineContext 를 사용하므로 다음과 같은 단점을 갖는다. 취소하기 어렵다. -> memory leak, cpu res 낭비 Parent 의 scope 속성을 상속하지 않는다. -> unit test 가 어렵다. # GlobalScope 을 사용하지 않기 위해 CoroutineScope 을 전달하는 경우가 있는데, 이는 또 다른 단점을 야기할 수 있다. 전달받은 scope 를 계속 다른 곳으로 전달하여 사용할 수 있으며, 이 경우 한 곳에서 발생한 exception 이 원하지 않는 다른 job 들을 모두 취소시킬 수 있다. (SupervisorJob 을 사용하지 않았다면) E.. 2022. 1. 29.
[Kotlin] Coroutine 에 대한 이해 : 기본 용어 및 사용 https://android.jlelse.eu/coroutines-basic-terminologies-and-usage-b4242bd1b2a4 Coroutine 이 뭐냐? -Coroutine 은 suspendable computaion instance 이다.코드 블럭이 life-cycle 을 가지고 생성되고 수행된다는 것이 개념상 thread 와 비슷하지만, 특정 thread 에 bound 되어 있지 않다.한 thread 에서 실행되고, 다른 thread 에서 resume 될 수도 있다.future 와 같이 result 나 exception 으로 종료될 수 있다. -Coroutine 은 lightweight thread 라고 볼 수 있다.여러개의 function 을 async 로 호출할 수 있고, 더 짧.. 2020. 8. 7.
[coroutine] Composing Suspending Functions ( suspending 함수 만들기 ) [coroutine] Composing Suspending Functions ( suspending 함수 만들기 ) https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/composing-suspending-functions.md#composing-suspending-functions Sequential by default - 2개의 suspending function 이 있다고 가정해보자. 그리고 그 두 개의 함수가 remote service call 이나 계산을 하는 등의 의미있는 행동을 하며, 시간이 좀 걸린다고 해보자. suspend fun doSomethingUsefulOne(): Int{ delay(1000L) // 의미있는 행동이라 가.. 2020. 3. 10.
[coroutine] coroutineScope 의 동작 특성을 알아보자. [coroutine] coroutineScope 의 동작 특성을 알아보자. - 아래 내용은 coroutine 에 대한 공부가 충분하지 않을 때 작성한 것이고, 22.10.13 기준 적당히 공부한 이후 한마디로 정리하면 아래와 같다. 더보기 coroutineScope 함수는 suspend function 으로 launch 등과 같은 coroutine builder 와는 다르다. suspend function 이므로 당연히 해당 함수 호출이 return 되어야 다음 구문이 실행된다. 그럼에도 아래 실험 결과들은 한번쯤 들여다볼만하다고 생각된다. - Coroutine 공식 가이드 문서에 있는 아래 샘플 결과를 보고 약간 멘붕에 빠졌다. 그래서 관련하여 실험한 결과를 여기에 정리해보고자 한다. fun main(.. 2020. 3. 8.
[coroutine] Coroutine Basics ( 코루틴 기초 ) https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/basics.md#coroutine-basics Your first coroutine - fun main(){ GlobalScope.launch{ delay(1000L) println("World!") } println("Hello,") Thread.sleep(2000L) } // Hello, // World! - 기본적으로 코루틴은 light-weight thread 이다. CoroutineScope 라는 context 안에서 "launch" 와 같은 coroutine builder 를 통해 시작된다. 위의 예제에서는 GlobalScope 라는 coroutine 을 사용해서 launch 를 했.. 2020. 3. 7.
Coroutine 과 놀아보기 #2 Coroutine 과 놀아보기 #2 - Coroutine 과 놀아보기 #1 과 마찬가지로 공부를 하면서 의아스러운 부분들을 테스트 코드를 통해 검증하며 정리한 글입니다.Coroutine 과 놀아보기 #1 - Job 이 cancel 되었을 때는 CancellationException 이 발생한다. 하지만, 이는 정상 동작으로 간주하여 coroutine 내에서는 crash 가 나진 않는다. ( runBlocking 안에서는 crash 가 난다.. runBlocking 은 따로 공부할 필요가 있다. ) - withTimeout 의 경우는 일단 synchronous 한 동작을 한다. (withContext 와 비슷하다.) 아래와 같은 코드가 있다면 어떤 로그도 찍히지 않는다. withTimeout(1000L){.. 2019. 5. 23.
[coroutine] exception 을 이해해보자 #1 ( in my coroutine scope ) -이 글은 coroutine exception 에 대한 일종의 심화과정이라고 볼 수 있다.기본 exception 에 대한 내용을 먼저 이해한 후 이곳의 글을 보자![coroutine] Exception handling -class MainActivity : Activity() { private val coroutineScope = CoroutineScope(Dispatchers.Main) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) findViewById(R.id.btn).setOnClickListener { try { co.. 2019. 3. 5.
[Coroutine] Coroutine context and dispatchers https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md#thread-local-data -기존에 있었던 doc 이 update 되어서 따로 정리한다. Coroutine context and dispatchers -Coroutine 은 항상 어떤 context 하에서 실행된다.이 context 는 CoroutineContext type 을 가지며 Kotlin standard lib 에 정의되어 있다. -Coroutine context 는 여러개의 element 들을 가진 set 이다.main element 는 coroutine 의 Job, dispatcher 이다. Dispatchers.. 2019. 2. 8.
반응형