Kotlin Flows in practice
ฝัง
- เผยแพร่เมื่อ 5 ก.พ. 2025
- With coroutines as the recommended solution for asynchronous operations, Kotlin Flow is the obvious choice for managing streams of data in Android apps. However, there are some lifecycle perils to consider and new lifecycle-aware APIs to understand in order to get the most out of Flows in Android. Learn about the powerful and expressive Flow APIs and how to use them across all layers of your app in a practical way-solving common use cases every developer can face.
Resources:
Kotlin flows on Android → goo.gle/3AXlXFi
Official Android Developers publication on Medium → goo.gle/3lUhY8o
Google I/O app source code → goo.gle/3ji3AFe
Speakers:
Manuel Vicente Vivo, Jose Alcérreca
Watch more:
Watch all the Android Dev Summit sessions → goo.gle/ads21-...
Watch all the Modern Android Development sessions → goo.gle/ads21-mad
Subscribe to Android Developers → goo.gle/Androi...
#AndroidDevSummit, #Kotlin #Featured #Latest
product: Android - Languages; event: Android Dev Summit 2021; fullname: Manuel Vicente Vivo, Jose Alcérreca; re_ty: Publish;
Wow. Its really amazing to see coroutine has evolved in different level now a days. Kudos to the team🙌🏻
Wow, that's an Amazing session of Basic understanding of Flow and State flow in the easiest form. very much needed this content because of the dilemma of Flow and Live data and what is flow where to start.
Thanks for the great content Liked it very much.
Wait till you hook up state flows with composable screens. match made in heaven
best toturial ever i watch about Flows
Nice video. You should cover Flows using Jetpack Compose
Love it the interactive way you did the video!
Wow. Really took a concept and broke it down. Thank you for this!
Best video on Flows on android!
Excellent content. Good to see that life cycle related pain points are addressed elegantly.
Very vivid explanation of the article
Thanks for the content, but I think there should be a clearer and a not breaking migration path from LiveData, the current suggestion of replacing `asLiveData` with `stateIn(WhileSubscribed(5000))` and `repeatOnLifecycle` won't give the same result, since the whole Flow chain will be re-executed when the apps comes from background, and this wasn't the case with `asLiveData`
@@manuelvivo9276 Thanks for the reply, the difference is that `asLiveData` won't restart the flow if it has been completed (since it uses the builder `liveData` under the hood), unlike the combination of `repeatOnLifecycle` + `stateIn(WhileSubscribed(5000))` which would restart the Flow everytime the app comes from background whatever the status of the upstream Flow.
So for long-living Flows: observing a database, or observing location updates..., there is no difference, but when the Flow is emitting a limited number of results (just fetching something from an API for example), using the new API, we will end up running it more than needed, we can solve this by using an intermediate `MutableStateFlow` to hold the results, instead of directly exposing the cold Flow, but in the video, you said that this solution is not reactive enough :)
@@manuelvivo9276 or we can keep using `launchWhenStarted` for those cases, since when using `Eager` or `Lazy` the upstream Flow will stay active in all cases, and the repeatOnLifecycle won't have any effect on it, instead it would just cause re-emitting the last value each time the app becomes active another time, and would cause some not-needed redrawing, so `launchedWhenStarted` would save more resources on this case.
Anyway, I think this scenario should be clarified in the docs, thanks again for the replies 🙏
تگنتتلتتتببببببتخبلتتبتتتتتتتتهتهبتتتختختا/88 ج/یالان إثخاأاآخااآآخااهاالتاالیتکیاثااتاتااالآاآحتلیآاالااعاتاتااااهاعاااعاااااتاااالهااعاخااالااااهاااااااالاعحاااالاتااهاااااااااخاخاتاااااااههااااخااهخهاهااختااخاااهااااهخااالااالاااالااااااااااهخااااالاهلهااهااااالاااااالالتاااااهاااالهااااتمخاتاهاعتاااااخاااهاااالالللاخخاهالخااهالاااهاهااااخلاعاتهلااههااخاااااخالختهااااالاهاااالاالاااهاااااللاالاااااخهااااااااالخهاهااهااااااااااااهااااااااااهاهلااااااااااخالااااالاااهااایاهاااااااااااخاخااهاالاااخاالاااهلعاهاهالااالاااااهالاااللاااااهلاااهااخااااالاهاالاااهاااااااهاهااهاااااااالاااهاااالااهااهاااااااااالالااااالااهاهههاهاخاااااللهاهاااااااااااالاایااااااااهلااااااااایااالاااللاعههععهااااهااااااهلاااااالاعااعااااهلاااعلاالعااعاخلااهااااااااااالالااخاالاااهالاااااااهخاهخاالاااالاهااااالاخلهعالالااههااااخالایاالکالاااهاااخالایااااااااااعااالالاااخلالاللعاالهالاااااییاخاخلاالاهااهااااااهعااخلالاعهایاااااححاااکهااایاهااااهاااعلاااههااااااههیهاااااااااهاهاااااااهایخااخئس
@@hichaam2 I follow this approach of having intermediate MutableStateFlow as a uiState in ViewModel. Compose UI uses collectAsState, I see collection happen only when UI is visibile. All the cold flow collections from domain/data are active when viewModel is active and update uiState. I am wondering what level of safety needs to be applied still.
@@arunjoseph662 to avoid this you have two options:
1. I wrote an article about the first one, which allows making the long-running Flows lifecycle aware, and stop them when the screen is inactive, google this "Making cold Flows lifecycle-aware"
2. Don't use intermediate MutableStateFlows, and instead compose the upstream Flows and use a `stateIn` operator, like what the video suggests, just be aware that this will cause the whole chain to be restarted each time the screen becomes active again.
this seems so seamless
Great work with this one guys!!
Thank you! You've helped me solve a lot of problems!
You are so welcome 😀
We're pleased to hear this has helped you on your Android journey 🛤️
What an amazing video! Thanks for the examples you were using they were very clear and adding lifecycle diagram is a top of the cherry...
Finally an alternative to rx java :)
nice presentation about the flow!!
Excellent content. Thanks to share it with us and now I can understand flow in Android context.
surprisingly this video actually helped.
Yeah nice video and Pancho is the best example, thank you :)
Very helpful with an excellent explanation
Very informative, thanks!
Delighted to hear this! Have you checked out the documentation for Kotlin Flows on Android?: goo.gle/3AXlXFi
Happy learning! 😎
10:46 This part I dont understand. Why is live data considered "adding another technology to the mix" (implying its not ideal), when before you said its lifecycle aware and perfectly fine?
Livedata comes from a different library, and you can do the same thing with repeatOnLifecycle api in flows only. That's why it's not ideal.
This was an awesome tutorial on flows! Thank you
Why do we need this alternative if we already have RxJava? You might as well have added lifecycle helpers for subscribing on lifecycle and that would pretty much be it
Great talk thank you folks
Cool,Thanks!Where are there flow for excample code?
Sure! You can check out our samples: goo.gle/3A5BvtJ or the new Now In Android app: goo.gle/3SxsIHU 😎
you can do what you want!
Muchas gracias! Good stuff and well explained! Keep on the good work. Thank you :)
can you explain the difference between statein(.... , whilesubscribed(5000) ) and collectAsStateWithLifecycle () are they supposed to do the same thing, because whilesubscirbed waits for 5 seconds ?
That was really good explanation guys. But all this was about reading data from source, error handling was not covered enough. I wonder how this fits into the real world apps with complicated views, validation etc.
That's were u use your own imagination and know how's
Well I understand that this short video cannot cover everything, but I think they should at least suggest what to watch/read later. Many programmers just follow these naive videos and hit the wall in the real world application
@@technics6215 that's what seperates the good developers and the average ones. I been on this game for over two decades and made stuff more complex without these new tools. Once you learn that, you can adapt, use these tools and expand on it to meet your needs or just go back to doing stuff before tools like these existed
nice example!! You the handsome guy really help me a lot
Amazing content, thank you very much to provide material that it so easy to be understanded!
If we use repeatOnLifecycle in activity, then importance of ViewModel likely less. So this example s just for illustration or really we don't need viewmodel anymore?
Great Video!
excelente explicacion 👌👌
Thanks for the great content! Will Flows replace the LiveData library entirely? It seems redundant now to have both of them since they do almost the same thing.
No, LiveData is still a great solution if you want to avoid having to understand everything in the video. It's recommended for beginners and simple cases. And of course, Java users.
@@JoseAlcerreca That is a great answer
Ok, about this 5 seconds timeout. The most common case you can only imagine - opening details screen from a list screen. Of course one spends more then 5 seconds on the details screen. Then user goes back, and SUDDENLY repository called again, as flow was recreated. How are you going to handle this?
Good point, still need another solution for "go back from details screen". Livedata does it well
The 5 seconds timeout is of no use to me here .for sure it's not efficient to run a loop to fetch data from a server ,what if the data has not changed ? Instead ,we can optimize the messaging services like FCM , and update the mutable state in the viewModel ,then collect this flow as state !
There is also a function called "launchWhenCreated". İsn't that same with "repeatOnLifecycle" ?
it's very different. launchWhen* functions suspend the coroutine while repeatOnLifeCycle cancels and relaunches the coroutine
amazing video
Need more examples on how to unit test Flows
cool video)
15:41 why would ViewModel recreate the flow if ViewModel is not recreated?
Because the activity which is re recreated, calls the collect method
Amazing
I guess is meaningless to use Flow with Firebase real time database since firebase onDataChange method of a ValueEventListener is always listening for updates.
thanks
9:14
it should be:
".. as called cold flows"
awesome
I wish Flutter uses Kotlin.
I love Flutter, but Dart sucks compared to Kotlin...
Hey, I want to build an app, can you teach me, huch much time it would take?
lifesaver
👍👍👍👍👍👍
How do i access Android/data folder and add some .bin files in other app directory. please tell me.
благодарю
WOW
Hello Mahmoud! If you'd like to learn more, please check out this Kotlin playlist: goo.gle/MAD-Kotlin
This is very advanced stuff for me. Real hard to understand. :(
"Pencho" - Virat kohli
Unnecessarily complicated. As always with Android, explanations start off easy with non real world implementations, but once you start combining their own Android APIs everything keeps tripping on their own feet.
Poor presentation.
The portion describing repeatOnLifecycle comparatively to lifecyclescope.launch() and lifecycleScope.launchWhenStarrted() is very confusing. The issues are not fully explained IMO and the recommended approach is to use the repeatOnLifecycle to avoid issues. What are the advantages of the other two and why are those advantages not explained in depth here? Or why have they even been added or deprecated?
Thanks for sharing!
Please add a "{" after " repeatOnLifecycle(Lifecycle.State.STARTED)"