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

Daniel Ryan

11/08/2021, 12:30 PM
Newbie question: when querying a database using a reactive driver, what are the advantages and differences in behaviour when returning a flow from a repository function, as opposed to returning a list from a suspend function?
k

krzysztof

11/08/2021, 12:43 PM
With a function approach, you have to call the function yourself to get the latest data from database. With Flow, your UI will automatically update when something changes within that db query
so with Flow, the advantage is that you don’t have to remember to call
getLatestDBEntries
to keep your UI up to date
d

Daniel Ryan

11/08/2021, 12:54 PM
Thanks. Some additional information: this is for a backend rest service that will not support polling. I was looking the example from https://www.baeldung.com/kotlin/spring-boot-kotlin-coroutines and I could not work out a reason why the repo returns a flow rather than a list from a suspend function in this example? As it is not using the repo for live updates, is there and reason/advantage to returning a flow in this example?
k

krzysztof

11/08/2021, 1:53 PM
I’m not familiar with
Flux
class here, but if I would have to guess,
getAllProducts
does not return a list - rather it’s an emitter of each item found. Probably that’s why Flow was used here
Quick search told me that
Flux
is part of
WebFlux
- reactive framework. That solves the mystery 😄
2 Views