I've started to learning some more "advanced" stuff, and I'm a bit overwhelmed. I'm not sure, if I understand things correctly. I'd like to know, when I should use the Flow, and when Corountines.
Let's say that I have two apps. The first one have a few fragments within it. Every fragment will make different api call, for example:
current forecast
weekly forecast
monthly forecast
api call will be done atleast once, since user can change data needed to perform an api call, for example - different city. Should I use corountines with this type of application?
The second application, will be based on database operations, let's say it'll be some kind of "journal", where user will be adding his meals, with some macronutrients. Since this application will be mostly about database, should I use Flow then?
What if I'll have an app, that store some data in database, but also performs the api calls? Should I "mix" flows (for database operations), and corountines (for api operations)?
I think you mean suspend functions rather than coroutines. Coroutines are used for Flows too...that's how you collect them.
Suspend functions are for fetching a piece of data once, imperatively. Flows are for passively receiving data repeatedly each time it is changed.
The difference is that with a reactive design (Flows), your input and output code is completely independent from each other. When the user changes something, you inform the class that manages your data and it will take care of updating Flows "under the hood". You separately collect the Flows and use incoming data to update UI. These two pieces of code don't have to coordinate with each other.
With imperative design (suspend functions), your code for input and output is together in one place. When the user changes something, you call a suspend function to fetch data using the new input, and you take the returned data and connect it to UI.
I can't say more than that without straying into opinion. You can choose the type of design you prefer, or use a mixture.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With