Created
April 14, 2018 11:06
-
-
Save tomkoptel/c5ebd5cbc1fdba524db4bc2eae0d3214 to your computer and use it in GitHub Desktop.
Example of launching 3 network requests in on coroutine block. Couroutine get created by artificial wrapper factory API.
This file contains hidden or 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(private val coroutines: CoroutinesProvider) : ViewModel() { | |
| fun load() { | |
| coroutines.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