Does anyone know what application scenarios there ...
# coroutines
u
Does anyone know what application scenarios there are for Dispathers.Unconfined in Kotlin coroutines?
r
I'm no expert, but found this statement on this page https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
The unconfined dispatcher is appropriate for coroutines which neither consume CPU time nor update any shared data (like UI) confined to a specific thread.
To me, that says it's only suitable for code which coordinates work of other coroutines, but does no real work of its own.
u
Right. Or no relevant amount of work. But coordination is a good candidate. Let’s say you are on the main dispatcher and call many io functions. It is a bit of an anti pattern to dispatch to Dispatchers.IO and thereby take the dispatcher switching off them. Each of those suspend functions should schedule themselves on Dispatchers.IO. Now without intervention, in your coordinator you would automatically dispatch back to the main Dispatcher between all those calls causing a big dispatching overhead. If you switch to Dispatcher.Unconfined, you do not take over the responsibility of those io functions to decide where to run and at the same time do not cause massive dispatcher changes.