Is there a difference between `async(<http://Dispa...
# coroutines
r
Is there a difference between
async(<http://Dispatchers.IO|Dispatchers.IO>) { ... }.await()
and
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... }
? And how can I ensure they don't run on the main thread (hello Android)?
j
`async`/`await` creates a new coroutine,
withContext
moves the current coroutine
🤔 1
👍 1
s
Is there a difference…?
Using
async
with an immediate
await()
doesn't make a lot of sense and isn't something I'd ever do deliberately. If you write that, IntelliJ IDEA will suggest you change it to the second version, with
withContext
.
And how can I ensure they don't run on the main thread?
By using
<http://Dispatchers.IO|Dispatchers.IO>
, you are telling it not to run on the main thread.
👍 1
j
Otherwise both are effective for doing work off of the main thread.
1
r
Hm, so something different escapes to the main thread
3