Zachary Siegel
06/27/2023, 6:22 AMsuspend fun foo() = coroutineScope {
val b = async { blockingHttpRequest() }
val s = async { suspendedHttpRequest() }
Pair(b.await(), s.await())
}
fun blockingHttpRequest() { ... }
suspend fun suspendedHttpRequest() { ... }
Given two long-running functions making network requests, one which is a normal blocking function, the other a suspend function, is there any difference between the execution of the two functions each within their own coroutine? What would be the benefit of using a suspended version?
I just read the docs four times over and can't find an answer.Sam
06/27/2023, 7:37 AMSam
06/27/2023, 7:38 AMZachary Siegel
06/27/2023, 2:38 PMSam
06/27/2023, 3:48 PMSam
06/27/2023, 3:51 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
. That will free up the calling thread while the blocking call takes place on one of the dedicated threads owned by the IO dispatcher. (In practice there might not be an actual thread switch, due to some clever implementation).Zachary Siegel
06/27/2023, 4:05 PM