Paul Woitaschek
12/23/2019, 10:49 AMDico
12/23/2019, 11:11 AMPaul Woitaschek
12/23/2019, 12:50 PMDispatchers.default
dekans
12/23/2019, 1:15 PMPaul Woitaschek
12/23/2019, 1:16 PM/**
* The default [CoroutineDispatcher] that is used by all standard builders like
* [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc
* if no dispatcher nor any other [ContinuationInterceptor] is specified in their context.
*
* It is backed by a shared pool of threads on JVM. By default, the maximal level of parallelism used
* by this dispatcher is equal to the number of CPU cores, but is at least two.
* Level of parallelism X guarantees that no more than X tasks can be executed in this dispatcher in parallel.
*/
@JvmStatic
public actual val Default: CoroutineDispatcher = createDefaultDispatcher()
dekans
12/23/2019, 1:18 PMval scope = MainScope()
scope.launch [ runsInMainThread() }
Because scope dispatcher is Dispatchers.MainPaul Woitaschek
12/23/2019, 1:18 PMdekans
12/23/2019, 1:18 PMif no dispatcher nor any other [ContinuationInterceptor] is specified in their context.
launch
if none specified in parametersDico
12/23/2019, 2:16 PMPaul Woitaschek
12/23/2019, 2:46 PMDispatchers.Default
not being Dispatchers.Main
is not rightAdam Powell
12/23/2019, 3:51 PMCoroutineScope
you launch into should have a dispatcher configured already. You should be creating scopes through standardized utilities so that you never have to think about it, e.g. LifecycleOwner.lifecycleScope
, ViewModel.viewModelScope
, or similar definitions you write yourselfDispatchers.Main
would make a poor default anyway; it would be an unnecessarily UI-centric perspective for a general tool like coroutines to adoptDispatchers.Default
to point at Dispatchers.Main
for your app, it would probably have catastrophic effects on the behavior of libraries you use that assume Dispatchers.Default
is a pool of worker threads for CPU-bound work. Those libraries would start blocking your main thread very quickly if you changed that assumption out from under them 🙂dekans
12/23/2019, 4:06 PMPaul Woitaschek
12/23/2019, 4:07 PM