Zoltan Demant
08/18/2022, 7:54 AMscope.launch {}
from MainScope? Generally, the task itself will run with the "correct" dispatcher, for example: loading something from a database is always executed using withContext(<http://Dispatchers.IO|Dispatchers.IO>)
.
But to me it seems that I might as well specify something like Dispatchers.Default
in the launch block, so that all tasks are at least off-loaded from the main thread by default? As a real life scenario, I was just working on an issue where UI froze after the user had selected a file to import from; I had failed to realize that contentResolver.openInputStream(uri) was blocking rather than reading from the inputStream itself. If I had been using Dispatchers.Default
, this wouldnt have been a problem. Now, instead I have to wrap each individual call like this (there arent many, but still) in a withContext(Dispatchers.Default/IO)
.Robert Williams
08/18/2022, 9:45 AMtseisel
08/18/2022, 9:46 AMMainScope
are expected to execute blocking operations. Sometimes you just need to execute suspend UI operations, for example waiting for a delay before retrying an operation.
suspend
functions should be main-safe, i.e. they should be designed to use another dispatcher internally, so that callers may run on any dispatcher without worying about blocking the main thread.Robert Williams
08/18/2022, 9:47 AMZoltan Demant
08/18/2022, 10:06 AM