Shalom Halbert
12/23/2020, 10:29 AMlaunch
is used, we use dispatchers IO
and Default
to run code asynchronously. Why is async
run asynchronously if you do not run on one of those dispatchers? It's possible I'm misunderstanding something.Circusmagnus
12/23/2020, 11:53 AMin different threads
It just means, that they do not depend on each other, are not synchronized in any way. They may still run in parallel OR be just interleaved on single thread.
suspend functions may, well... suspend their execution - waiting for something. This does not block a thread. So, during this suspension, other functions / coroutines may be run on this thread. And, when they finish, our suspend function may resume in the very same thread.
Considering code
suspend fun one() {
delay(100)
}
suspend fun two() {
callSomeOtherSuspendFun
}
singleThreadScope.launch {
doSth()
one()
doSomeOtherThing()
}
singleThreadScope.async { two() }
We are on single thread. The launch will start first. But soon enough it will suspend for 100 milliseconds. From this moment our async may start executing. It too cuould get suspended, because it is calling suspending function - making room for launch to resume its execution.
So conceptually, both launch and async are executing at the same time. We cannot know, which one will finish first. They are asynchronous, even, when run on single thread.
viewmodelScope
(which is run on Dispatchers.Main). They will soon enough suspend, waiting for network calls / db queries to complete. And while they are suspended, other data fetching-corotuines may run on Main Thread. What is more - UI can run on Main thread too, uninterrupted.
As long as you are not calling any blocking functions, you are free to go on Main Thread, and just forget about threading completely.
*blocking function - function, which will execute for a considerable time, bloking a thread. suspend functions exposed by Room or Retrofit are not blocking, and can be called on Main Thread no problem.withContext()
• wrap non-blocking callback APIs in suspendCancellableCorotuine
- which will make coroutine suspend until called back by callback
• launch
and async
without any Dispatcher specified. If execution needs to be switched to different thread, assume, that suspend fun
, which you are calling, has taken care of that (via withContext()
or suspendCancellableCoroutine
)streetsofboston
12/23/2020, 1:53 PM