Created
July 29, 2020 14:39
-
-
Save tizisdeepan/294d24ee18592b3f45f91ce545f2488d to your computer and use it in GitHub Desktop.
MVVM with MediatorLiveData
This file contains 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
onCreate() { | |
val viewModel = ... //initialize your view model | |
viewModel.data.observe(this, Observer { | |
//do operations based on the value | |
}) | |
viewModel.getData() | |
} |
This file contains 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
fun getData(): Flow<Data> = flow { | |
//do network calls | |
emit(data) | |
}.flowOn(Dispatcher.IO) |
This file contains 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
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