Skip to content

Instantly share code, notes, and snippets.

@egorshustov
Created January 10, 2019 05:30
Show Gist options
  • Save egorshustov/9f9f642b4a85c31c95a705abe6b66789 to your computer and use it in GitHub Desktop.
Save egorshustov/9f9f642b4a85c31c95a705abe6b66789 to your computer and use it in GitHub Desktop.
Two ways to create async functions in ViewModel class
class LoginViewModel(context: Application) : AndroidViewModel(context) {
...
fun isUserExists(id: Int) = viewModelScope.async {
usersRepository.isUserExists(id)
}
// But you can also write this way:
suspend fun isUserExists(id: String): Int {
val res = viewModelScope.async {
usersRepository.isUserExists(id)
}
return res.await()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment