본문 바로가기
[android] 안전하게 flow 를 collect 하기! - android 에서는 보통 UI layer 에서 flow 가 collect 된다. LifecycleOwner.addRepeatingJob, Lifecycle.repeatOnLifecycle, Flow.flowWithLifecycle 등을 이용해서 쓸데없이 res 가 낭비되지 않도록 하는 방법에 대해 알아본다. 리소스 낭비 - CoroutineScope.launch, Flow.launchIn, LifecycleCoroutineScope.launchWhenX 를 사용하는 것은 Activity 가 bg 로 들어갈 때 Job 을 수동으로 취소해주지 않는 한 안전한 방법이 아니다. 위 API 들은 app 이 bg 상태에 있을 때도 flow producer 가 buffer 에 emit 을 하도록 하고, 따라서 .. 2021. 5. 11.
[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 과 놀아보기 #1 Coroutine 과 놀아보기 #1 - android studio 에서 coroutine 관련된 것들을 인식 못한다. android 3.2.1 버전으로 업그레이드 하니 해결되었다. 해당 버전 이상으로 환경 세팅을 하고 시작하자! - 이 글은 내가 coroutine 을 학습한 후에 약간 까리한 부분들을, 테스트 코드를 짜서 실험한 결과를 공유하는 글이다. runBlocking? 새로운 coroutine 을 수행하며, 작업이 끝날 때까지 현재 thread 를 block 시킨다. (interrupt 가능하다.) - 다음과 같은 테스트를 해보았다. btn.setOnClickListener{ runBlocking{ startLogPrinting() // mainHandler 에서 50ms 마다 “logPrinti.. 2019. 5. 22.
[coroutine] exception 을 이해해보자 #2 ( in global scope ) -[coroutine] exception 을 이해해보자 #1 ( in my coroutine scope ) 과 동일한 sample 들을 직접 생성한 coroutine 이 아닌 GlobalScope 에서 수행해보기로 했다.혹시나 뭔가 동작이 다를까 해서..내가 알고 있기로는 GlobalScope 은 defaultHandler 가 setting 되어 결국 모든 exception 을 먹어 crash 가 나지 않는다는 차이만 있는것으로 알고 있는데.. 검증해보자 ( 결론은 잘못 알고 있었다. ) -class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState).. 2019. 3. 6.
[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] Exception handling Exception propagation - Coroutine builder 는 2가지 flavor 가 있다. exception 을 자동으로 전파하는 launch, actor류. 그리고 user 에게 노출되는 async, produce 류. 자동 전파하는 케이스는 Java 의 uncaughtExceptionHandler 와 비슷하게 unhandled exception 이다. 반면에 user 에게 노출되는 경우는 user 가 exception 을 직접 handle 할 수 있다. (돼왕: try-catch 할 수 있다.) 대표적 api 는 await 와 receive 가 있다. - runBlocking{ val job = GlobalScope.launch{ println(“Throwing exception f.. 2019. 3. 4.
[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.
[Kotlin] Coroutine 소개 (그렇지만 조금 깊을 수도 있음..) [Kotlin] Coroutine 소개 https://www.slideshare.net/elizarov/introduction-to-kotlin-coroutines -Coroutine 개념은 아주 오래된 녀석이다.Simula 67 이란 언어에서 처음으로 소개된 개념이다. detach : suspension statement resume : resume coroutine execution -Coroutine 은 multithreading 이 나오면서 잠시 out 되어있었다.그러나 최근에 async code 가 많이 사용되면서 다시 주목받기 시작했다. -Thread 는 resource 측면에서 high-load 이며,single thread 만 지원하는 언어들도 있으며,mutable state 가 많고, U.. 2018. 11. 27.
[Kotlin] Coroutine vs. thread (Light-weight thread 가 뭔 말이야?) [Kotlin] Coroutine vs. thread (Light-weight thread 가 뭔 말이야?) https://stackoverflow.com/questions/43021816/difference-between-thread-and-coroutine-in-kotlin -Coroutine 은 stackless, stackful 이렇게 두 가지 type 이 있다.Kotlin 은 stackless coroutine 이다. stackless 이기 떄문에 약간의 기능제한이 있다. -https://www.quora.com/Why-is-it-that-with-a-stackless-coroutine-only-the-top-level-routine-may-be-suspended Stackless corouti.. 2018. 11. 26.
반응형