Skip to content

Instantly share code, notes, and snippets.

@shkschneider
Created November 27, 2018 09:23
Show Gist options
  • Save shkschneider/fe2cc0610ebffda9d758b6853188fbac to your computer and use it in GitHub Desktop.
Save shkschneider/fe2cc0610ebffda9d758b6853188fbac to your computer and use it in GitHub Desktop.
@UiThread
fun getUsers(context: Context): LiveData<List<User>> {
if (!::users.isInitialized) {
users = MutableLiveData()
users.postValue(MyDatabase.get(context).users().getAll())
GlobalScope.launch(Dispatchers.Main) {
val usersFromDb: List<User> = async(Dispatchers.IO) {
return@async MyDatabase.get(context).users().getAll()
}.await()
users.value = usersFromDb
}
}
return users
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment