Skip to content

Instantly share code, notes, and snippets.

@charlesmuchene
Last active March 13, 2020 17:51
Show Gist options
  • Save charlesmuchene/88d78eac8f8ca011d25e00c5a2b0524d to your computer and use it in GitHub Desktop.
Save charlesmuchene/88d78eac8f8ca011d25e00c5a2b0524d to your computer and use it in GitHub Desktop.
Network call using coroutines
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