Does nesting `withContext(IO)` inside another `wit...
# coroutines
f
Does nesting
withContext(IO)
inside another
withContext(IO)
produce a thread switch, even if both use the same dispatcher? Or, when exiting the nested
withContext(IO)
, is it guaranteed that execution will continue on the same thread without a switch, since the dispatcher is the same?
🚫 1
s
The answer above (emoji) says "no". However, IIRC, when suspend and then resuming within the same dispatcher, there is thread _affinity_: The thread will very likely be the same, no switch is done. However, I'm not sure that this is a 100% guarantee.
k
AFAIK there's no guarantee that the thread will stay the same. That's why in Android we have
Dispatchers.Main.immediate
which is the only one which gives that guarantee.
👍 1