Skip to content

Instantly share code, notes, and snippets.

@tizisdeepan
Created July 29, 2020 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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