Skip to content

Instantly share code, notes, and snippets.

@tizisdeepan
Created July 29, 2020 14:39
Show Gist options
  • Save tizisdeepan/294d24ee18592b3f45f91ce545f2488d to your computer and use it in GitHub Desktop.
Save tizisdeepan/294d24ee18592b3f45f91ce545f2488d to your computer and use it in GitHub Desktop.
MVVM with MediatorLiveData
onCreate() {
val viewModel = ... //initialize your view model
viewModel.data.observe(this, Observer {
//do operations based on the value
})
viewModel.getData()
}
fun getData(): Flow<Data> = flow {
//do network calls
emit(data)
}.flowOn(Dispatcher.IO)
val data: MediatorLiveData<Data> = MediatorLiveData()
fun getData() {
data.addSource(repository.getData().onStart {
//do something
}.asLiveData(viewModelScope.coroutineContext)) {
data.value = it
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment