Skip to content

Instantly share code, notes, and snippets.

@tizisdeepan
Created July 29, 2020 14:11
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/41b6b50b6d38a50d0b384d48b19f6ac8 to your computer and use it in GitHub Desktop.
Save tizisdeepan/41b6b50b6d38a50d0b384d48b19f6ac8 to your computer and use it in GitHub Desktop.
MVVM with Kotlin Flow
onCreate() {
val viewModel = ... //initialize your view model
viewModel.getData()
viewModel.data.observe(this, Observer {
//do operations based on the value
})
}
fun getData(): Flow<Data> = flow {
//do network calls
emit(data)
}.flowOn(Dispatcher.IO)
val data: LiveData<Data> = MutableLiveData()
fun getData() {
data = repository.getData().onStart {
//do something
}.asLiveData(viewModelScope.coroutineContext)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment