Zoltan Demant08/18/2022, 7:54 AM
from MainScope? Generally, the task itself will run with the "correct" dispatcher, for example: loading something from a database is always executed using
. But to me it seems that I might as well specify something like
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
, this wouldnt have been a problem. Now, instead I have to wrap each individual call like this (there arent many, but still) in a
Robert Williams08/18/2022, 9:45 AM
tseisel08/18/2022, 9:46 AM
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.
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 Williams08/18/2022, 9:47 AM
Zoltan Demant08/18/2022, 10:06 AM