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