Created
January 10, 2019 05:30
-
-
Save egorshustov/9f9f642b4a85c31c95a705abe6b66789 to your computer and use it in GitHub Desktop.
Two ways to create async functions in ViewModel class
This file contains hidden or 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
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