https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
m

mattinger

02/02/2023, 9:44 PM
Does anyone know if you create a retrofit interface with a suspending function, does retrofit automatically switch context to the IO dispatcher behind the scenes?
k

kevin.cianfarini

02/02/2023, 9:45 PM
The suspend functions are main safe. Operations are offloaded to an executor that’s an internal implementation detail of OkHttp
e

ephemient

02/02/2023, 9:45 PM
yep. it's not the
<http://Dispatchers.IO|Dispatchers.IO>
, but it does context switch and it is safe.
m

mattinger

02/02/2023, 9:46 PM
Thanks. That was my assumption, but i wanted to double check as one of my teammates asked in a code review.
k

kevin.cianfarini

02/02/2023, 9:46 PM
The only difference this would make to you is that you potentially won’t get “free” context switches between
Dispatchers.Default
and
<http://Dispaters.IO|Dispaters.IO>
, but that’s not a very big deal.
e

ephemient

02/02/2023, 9:59 PM
you could try to adapt kotlinx.coroutines.CoroutineDispatcher to okhttp3.Dispatcher, but digging into the Retrofit code, it will always suspend to avoid issues with throwing undeclared exceptions from java.lang.reflect.Proxy instances anyway, so it's kind of moot
8 Views