Last active
April 14, 2018 11:00
-
-
Save tomkoptel/2c6609e21c5f4090b7a7e0f3b264bf4a to your computer and use it in GitHub Desktop.
Example of launching 3 network requests in on coroutine block.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.experimental.launch | |
import kotlinx.coroutines.experimental.android.UI | |
import kotlinx.coroutines.experimental.async | |
class Model : ViewModel() { | |
fun load() { | |
launch(UI) { | |
// Start 3 calls in parallel | |
val response1 = api.makeAcall() | |
val response2 = api.makeAcall() | |
val response3 = api.makeAcall() | |
// Wait for result and offload work to background thread | |
val result = async { | |
combine(response1.await(), response2.await(), response3.await()) | |
}.await() | |
render(result) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment