Skip to content

Instantly share code, notes, and snippets.

@jeremyrempel
Created June 29, 2019 17:48
Show Gist options
  • Save jeremyrempel/95c38c7c5465bda2fc4c62bca4434ace to your computer and use it in GitHub Desktop.
Save jeremyrempel/95c38c7c5465bda2fc4c62bca4434ace to your computer and use it in GitHub Desktop.
class MainFragmentViewModel @Inject constructor(myService: MyService) : ViewModel() {
private val data: MutableLiveData<String> = MutableLiveData()
init {
// call async service with callback
myService.getData(data::setValue)
}
fun getData(): LiveData<String> = data
}
class MyService @Inject constructor() {
// do something async and respond via callback. Could be RX, Coroutine or ExecutorService
fun getData(onComplete: (String) -> Unit) {
return onComplete("Hello From the Future")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment