2024/05 5

Android - Coroutines best practices

공식문서에서 제시한 순서대로 나열 1. Inject Disptachers새로운 코루틴을 만들거나, withContext 를 호출할때 디스패처 하드코딩 피해라. 테스트 할때 TestDispatcher 주입하는게 더 쉬워진다. Note: The viewModelScope property of ViewModel classes is hardcoded to Dispatchers.Main. Replace it in tests by calling Dispatchers.setMain and passing in a test dispatcher. 2. Suspend functions should be safe to call from the main thread suspend function 을 호출하는 호출부에서 어떤 D..

Android 2024.05.15

3. Kotlin Coroutine - Concurrency issues

https://medium.com/androiddevelopers/coroutines-on-android-part-iii-real-work-2ba8a2ec2f45 코루틴은 어떻게 코드를 단순화하고, main-safety 를 제공하고, leaking work 를 피할 수 있는지 등을 살펴봤다.  실제 어떤 작업에 코루틴을 써먹는게 좋냐? 1. One shot request : 호출 될 때 마다 실행되는 작업. 결과가 준비되면 완료된다. 2. Streaming requests : 변화를 지속적으로 관찰하고 호출자에 이를 report 한다. 첫 결과가 준비되더라도 끝나지 않는다.  One shot request 의 구체적인 예시를 살펴보자. - 정렬된 목록을 사용자에게 보여주는 경우class ProductsV..

Android 2024.05.15

2. Kotlin Coroutine - Structured Concurrency, viewModelScope, coroutineScope

https://medium.com/androiddevelopers/coroutines-on-android-part-ii-getting-started-3bff117176dd 복습 해보자. 코루틴은 어떤 문제를 해결하는가? 1. 메인스레드 위에서 돌아가기에는 너무 긴 작업들에 대해서 처리2. Main-safety 로 인해 suspend 함수들이 메인스레드에서 호출 가능  그래서 코루틴은 일반 함수의 연산(invoke, return) 외에 suspend, resume 연산까지 갖는다.  코루틴 관리하기 (Keeping track of coroutines) 코루틴을 왜 관리해야 할까? 코루틴 자신은 수행되는 작업을 관리하도록 도와주지 않음. 코루틴을 아주 많이 실행시키는 것은 물론 가능하지만, 비싼 연산을 하고..

Android 2024.05.15

1. Kotlin Coroutine - suspend, resume, Dispatchers.

https://medium.com/androiddevelopers/coroutines-on-android-part-i-getting-the-background-3e0e54d20bb 코루틴의 개념의 첫 구현체는 Simula (1967)  Android 에서 코루틴의 역할은 뭘까? 1. Main thread 를 블럭하는 긴 태스크 (long-running task) 처리 2. Main-safety 는 어떤 suspend function 이든 main thread 에서 호출될 수 있도록 함 suspend 키워드는 코틀린 컴파일러에 특수한 처리를 가하기 위한 용도. suspend function 은 일반 함수와 달리 두 가지 연산이 더 있음. 일반 함수에서 제공하는 invoke (or call), return ..

Android 2024.05.15

요즘의 다이어트 식단

- 스크램블 에그 - 팽이버섯 볶음 : 올리브유도 필요 없어서 좋다- 양배추/당근/양파/샐러리 볶음 : 각각 단 맛 & 향이 있어서 질리지 않게 먹을 수 있음. 대신 볶을 때 올리브유를 아주 조금이라도 넣는게 좋다. - 닭가슴살 구이 : 냉동실에서 꺼내서 바로 팬에 넣어 구워도 나름 먹을 만 하다. - 냉동 새우 볶음 : 야채볶음과 함께 먹기 괜찮은 메뉴

Home Cooking 2024.05.15