Skip to content

Instantly share code, notes, and snippets.

@charlesmuchene
Last active March 13, 2020 17:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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