https://kotlinlang.org logo
#flow
Title
# flow
s

Saul Wiggin

12/09/2020, 11:06 AM
How should I be replacing LiveData in a database call with flow?
j

Javier

12/09/2020, 11:09 AM
Room?
s

Saul Wiggin

12/09/2020, 12:41 PM
Hi Javier. The database is in Room. Replace LiveData<> with Flow<> as the datatype in the DAO. Use emit instead of postValue and collect instead of observe in the repository. Call the repository from the View Model and then call collect from the Fragment with GlobalScope.launch. Is that correct?
j

Javier

12/09/2020, 12:44 PM
you have an extension function to transform the flow into livedata in the ViewModel
having your domain and data layer with Flow
if you want to replace livedata in the viewmodel with a flow, you should transform the flow into stateFlow for example
s

Saul Wiggin

12/09/2020, 12:46 PM
Thanks. I’ll look into extention functions and stateFlow
g

gildor

12/09/2020, 2:41 PM
GlobalScope in Fragment doesn't sound right, you probably need lifecycleScope
2 Views