Last active
March 13, 2020 17:51
-
-
Save charlesmuchene/88d78eac8f8ca011d25e00c5a2b0524d to your computer and use it in GitHub Desktop.
Network call using coroutines
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
interface ApiService { | |
@GET("status") | |
suspend fun getStatus(): Response<Status> | |
} | |
class LearningCoroutinesViewModel @Inject constructor(private val apiService: ApiService) : | |
ViewModel() { | |
fun getStatus() { | |
// Launch the coroutine within the | |
// ViewModel's Scope using the IO Dispatcher | |
viewModelScope.launch(Dispatchers.IO) { | |
networkCall() | |
} | |
} | |
private suspend fun networkCall() { | |
val status = apiService.getStatus(/* parameters ... */) | |
// Do something awesome with the status | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment