Kotlin Flows (easy explained)
A Flow is a stream of data of the same type. They are lazy ( it will only start when someone requires it.), asynchronous (they are computed on coroutines) and sequential (flow goes one after the other) and there are normally 3 entities involved in the stream of data.
- The producer of the stream.
- Intermediaries (optional) that can modify the stream in some way.
- And, the consumer of the stream.
An Automatic Ice Dispenser Analogy
How can you remember easily all of the flow properties? Well, just think of an automatic ice dispenser.
Imagine that you want to fill a glass of ice from your automatic fridge’s dispenser.
In this case, the dispenser is the producer of the data, and the data here are the ice cubes, sadly you can’t get vodka from there, just ice cubes, which tell us that we can get only the same type of product. The consumer is the glass where the ice will end and there can be an optional intermediary between the producer and the consumer, for this you can imagine that you can put your hand between the dispenser and the glass, therefore altering the flow of ice in some way. Also, the flow of ice of the dispenser is lazy, meaning that it will only start when someone requires it and that is until you place your glass (consumer) under the dispenser, otherwise nothing will come out. Another of the characteristic of a flow is that is sequential, remembering that this means that the flow of data goes one by one, like in the ice dispenser, the first ice cube has to go out for the second to come out after, then the third ice cube and so on. Finally, we have to remember that flows are asynchronous, and as the dispenser is automatic, you could leave the glass there while you check your phone or do other stuff while the dispenser is working.
And that is a simple way you can remember the most important characteristics of a flow!