안드로이드에서 Coroutine과 Retrofit 및 Room을 함께 사용 시 주의사항
- Retrofit에서 코루틴 사용을 위해서는 2.6.0버전 이상을 사용해야 한다.
- Room과 Retrofit은 자체적으로 비동기 처리를 하기 때문에 Main Dispatcher에서 suspend function을 사용하더라도
main-safe하다. IO Dispatcher를 사용하는 것은 아니고 자체 Dispatcher를 사용함.
Suspend function support requires Retrofit 2.6.0 or higher.
Both Room and Retrofit make suspending functions main-safe.
It's safe to call these suspend funs from Dispatchers.Main, even though they fetch from the network and write to the database.
Both Room and Retrofit use a custom dispatcher and do not use Dispatchers.IO.
Room will run coroutines using the default query and transaction Executor that's configured.
Retrofit will create a new Call object under the hood, and call enqueue on it to send the request asynchronously.
참고자료)
1. Android Codelab
Use Kotlin Coroutines in your Android App | Android 개발자
In this codelab, you'll build a coroutine-based Android app that uses Architecture Components to fetch data from the network and a Room database.
developer.android.com