Android Jetpack

Coroutine - 2 (Dispatchers)

개복치 개발자 2022. 2. 22. 00:20

개복치개발자 강의는 아래의 링크에서 확인할 수 있습니다.

 

개복치개발자 | Linktree

uyalae@naver.com

linktr.ee

 

 Dispatchers라는 애를 코루틴을 사용하다보면 보게 됩니다.

 

 얘가 뭐냐하면 이전에 봤던 애인데 Dispatchers.IO라고 나와있습니다.

 

https://philosopher-chan.tistory.com/1483

 

 얘가 뭔지 공식문서를 찾아보면 아래와 같이 나와있습니다.

 

https://developer.android.com/kotlin/coroutines/coroutines-adv

 

 여기 보면 Dispatchers.IO 라고 나와있습니다.

 

 찾아보면 운행관리원/배치담당자 라고 나왔는데, 뭘 관리하냐라고 하면 당연히 코루틴을 관리합니다.

 

 

 어떻게 관리하는지는 아래의 블로그를 한번 참고해보시면 더 좋을 것 같습니다.

 

https://kotlinworld.com/141?category=954901 

 

[Coroutine] 3. Coroutine의 Dispatcher 란 무엇인가?

Coroutine을 공부하면서 Dispatcher에 대해 상세히 설명된 글이 없어서 이 글을 작성하게 되었다. 많은 사람들에게 도움이 되길 바란다. Dispatcher  코루틴을 시작하게 되면, Dispatcher란 단어를 가장 먼

kotlinworld.com

 

 자, 그러면 대충은 알겠는데, 하나만 더 보고 가겠습니다.

 

 저 Dispatchers.Main이라는게 뭘까, UI랑 상호작용하고 이런 말이 써있는데, 이 친구가 Main Thread에서 돌아가나? 라는 생각을 하게 됩니다.

 

 자 그렇다면 여기에서 아래에 코루틴에 대해서 보면은

 

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-main.html

 

 여기에서 보면

 

A coroutine dispatcher that is confined to the Main thread operating with UI objects. Usually such dispatchers are single-threaded.

 

UI 개체로 작동하는 메인 스레드로 제한된 코루틴 디스패처입니다. 일반적으로 이러한 디스패처는 단일 스레드입니다.

 

 메인쓰레드라고 나옵니다.

 

 자, 여기에서 잠깐 ViewModelScope를 한번 보겠습니다.

 

 

Dispatchers.Main.immediate

 

 여길 보면 위와 같이 나와있습니다.

 

 그런데 viewModelScope에서 Retrofit을 사용하는 경우가 있는데, 이 것을 Main Thread에서 잘 동작하는가? 에 대한 의문이 들 수 있습니다.

 

 이 의문에 대한 답은 아래의 글을 참고해보시면 됩니다.

 

https://stackoverflow.com/questions/61215947/retrofit-call-in-kotlin-coroutines-viewmodelscope

 

 

 

- 참조

 

https://stackoverflow.com/questions/61215947/retrofit-call-in-kotlin-coroutines-viewmodelscope

 

Retrofit call in Kotlin Coroutines viewModelScope

Recently I've updated my ViewModel to use new viewModelScope. From its' implementation, I see that Dispatchers.Main.immediate is set as the default CoroutineDispatcher for viewModelScope. So when

stackoverflow.com

https://gift123.tistory.com/60

 

안드로이드 개발 (30) viewModelScope

Android 는 현재 집중적으로 Coroutine 을 밀고 있습니다. Android에서 Coroutine을 응용한 api와 Coroutine 관련된 코드 스니펫 등등이 등장하면서 앞으로 Android에서 Coroutine을 활용할일이 많아지고 있습니다.

gift123.tistory.com

 

https://developer.android.com/kotlin/coroutines/coroutines-adv

 

Kotlin 코루틴으로 앱 성능 향상  |  Android 개발자  |  Android Developers

Kotlin 코루틴으로 앱 성능 향상 Kotlin 코루틴을 사용하면 네트워크 호출이나 디스크 작업과 같은 장기 실행 작업을 관리하면서 앱의 응답성을 유지하는 깔끔하고 간소화된 비동기 코드를 작성할

developer.android.com

 

https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/KotlinExtensions.kt

 

GitHub - square/retrofit: A type-safe HTTP client for Android and the JVM

A type-safe HTTP client for Android and the JVM. Contribute to square/retrofit development by creating an account on GitHub.

github.com

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-main.html

 

Main

Main commonjsjvmnative A coroutine dispatcher that is confined to the Main thread operating with UI objects. Usually such dispatchers are single-threaded. Access to this property may throw an IllegalStateException if no main dispatchers are present in the

kotlin.github.io

 

'Android Jetpack' 카테고리의 다른 글

Coroutine - 4 (Retrofit + Coroutine)  (0) 2022.03.02
Coroutine - 3 (Room + Coroutine)  (1) 2022.02.27
Coroutine - 1 (Coroutine이란?)  (0) 2022.02.20
Jetpack Navigation - 6 (Animation)  (0) 2022.02.19
Jetpack Navigation - 5 (Safe Args)  (0) 2022.02.19