Does anyone know if you create a retrofit interfac...
# squarelibraries
m
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
The suspend functions are main safe. Operations are offloaded to an executor that’s an internal implementation detail of OkHttp
e
yep. it's not the
<http://Dispatchers.IO|Dispatchers.IO>
, but it does context switch and it is safe.
m
Thanks. That was my assumption, but i wanted to double check as one of my teammates asked in a code review.
k
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
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