Fill the blank
Make this repository call main-safe. The SDK call blocks.
class ReportRepository(private val io: CoroutineDispatcher, private val sdk: LegacySdk) {
suspend fun report(): String = ____(io) {
sdk.downloadBlocking()
}
}Hint 1 of 1
Which builder returns the block's result?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A withContext
Correct. It switches to the IO dispatcher, waits, and returns the result to the caller.
Why not the others?
- B launch. launch returns a Job, not the String, and isn't available outside a scope.
- C async. async needs a scope and returns a Deferred, not the String.
- D runBlocking. That blocks the caller's thread, which is the opposite of main-safe.