Kotlin Flows in practice

  Рет қаралды 151,714

Android Developers

Android Developers

2 жыл бұрын

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-allsessions
Watch all the Modern Android Development sessions → goo.gle/ads21-mad
Subscribe to Android Developers → goo.gle/AndroidDevs
#AndroidDevSummit, #Kotlin #Featured #Latest
product: Android - Languages; event: Android Dev Summit 2021; fullname: Manuel Vicente Vivo, Jose Alcérreca; re_ty: Publish;

Пікірлер: 84
@karthikss6501
@karthikss6501 2 жыл бұрын
Wow. Its really amazing to see coroutine has evolved in different level now a days. Kudos to the team🙌🏻
@MuhammadFarhan-pq2wc
@MuhammadFarhan-pq2wc 2 жыл бұрын
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.
@jonneymendoza
@jonneymendoza Жыл бұрын
Wait till you hook up state flows with composable screens. match made in heaven
@deepakbisht4957
@deepakbisht4957 2 жыл бұрын
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...
@thecsciworker291
@thecsciworker291 2 жыл бұрын
Wow. Really took a concept and broke it down. Thank you for this!
@vengateshm2122
@vengateshm2122 2 жыл бұрын
Excellent content. Good to see that life cycle related pain points are addressed elegantly.
@chuka_obi5167
@chuka_obi5167 2 жыл бұрын
This was an awesome tutorial on flows! Thank you
@HaSeebpjr01
@HaSeebpjr01 4 ай бұрын
best toturial ever i watch about Flows
@theredd1703
@theredd1703 8 ай бұрын
Love it the interactive way you did the video!
@thiagolopessilva
@thiagolopessilva 2 жыл бұрын
Excellent content. Thanks to share it with us and now I can understand flow in Android context.
@ILikeActions
@ILikeActions 2 жыл бұрын
Amazing content, thank you very much to provide material that it so easy to be understanded!
@goobar
@goobar 2 жыл бұрын
Great work with this one guys!!
@thomasknee7844
@thomasknee7844 2 жыл бұрын
Great video, very informative and helped me understand things much better
@hoonjung2143
@hoonjung2143 2 жыл бұрын
Muchas gracias! Good stuff and well explained! Keep on the good work. Thank you :)
@jeffreynyauke7636
@jeffreynyauke7636 2 жыл бұрын
Nice video. You should cover Flows using Jetpack Compose
@kreutzerzetanism7396
@kreutzerzetanism7396 11 ай бұрын
this seems so seamless
@anteckoeros
@anteckoeros 2 жыл бұрын
Yeah nice video and Pancho is the best example, thank you :)
@xeon3325
@xeon3325 Жыл бұрын
Very vivid explanation of the article
@dithyramb6152
@dithyramb6152 Жыл бұрын
Thank you! You've helped me solve a lot of problems!
@AndroidDevelopers
@AndroidDevelopers Жыл бұрын
You are so welcome 😀 We're pleased to hear this has helped you on your Android journey 🛤️
@megaxenu753
@megaxenu753 10 ай бұрын
surprisingly this video actually helped.
@navczydev
@navczydev 2 жыл бұрын
Great talk thank you folks
@Padpaad
@Padpaad 2 жыл бұрын
nice presentation about the flow!!
@nima7605
@nima7605 Жыл бұрын
Very helpful with an excellent explanation
@Mrdresden
@Mrdresden 2 жыл бұрын
Very informative, thanks!
@AndroidDevelopers
@AndroidDevelopers Жыл бұрын
Delighted to hear this! Have you checked out the documentation for Kotlin Flows on Android?: goo.gle/3AXlXFi Happy learning! 😎
@iandamping9757
@iandamping9757 2 жыл бұрын
Great Video!
@sureshmiyani722
@sureshmiyani722 Жыл бұрын
you can do what you want!
@witoldsienski1709
@witoldsienski1709 2 жыл бұрын
Finally an alternative to rx java :)
@jeckonly5853
@jeckonly5853 2 жыл бұрын
nice example!! You the handsome guy really help me a lot
@hichaam2
@hichaam2 2 жыл бұрын
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`
@hichaam2
@hichaam2 2 жыл бұрын
@@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 :)
@hichaam2
@hichaam2 2 жыл бұрын
@@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 🙏
@lalesaboori4353
@lalesaboori4353 2 жыл бұрын
تگنتتلتتتببببببتخبلتتبتتتتتتتتهتهبتتتختختا/88 ج/یالان إثخاأاآخااآآخااهاالتاالیتکیاثااتاتااالآاآحتلیآاالااعاتاتااااهاعاااعاااااتاااالهااعاخااالااااهاااااااالاعحاااالاتااهاااااااااخاخاتاااااااههااااخااهخهاهااختااخاااهااااهخااالااالاااالااااااااااهخااااالاهلهااهااااالاااااالالتاااااهاااالهااااتمخاتاهاعتاااااخاااهاااالالللاخخاهالخااهالاااهاهااااخلاعاتهلااههااخاااااخالختهااااالاهاااالاالاااهاااااللاالاااااخهااااااااالخهاهااهااااااااااااهااااااااااهاهلااااااااااخالااااالاااهااایاهاااااااااااخاخااهاالاااخاالاااهلعاهاهالااالاااااهالاااللاااااهلاااهااخااااالاهاالاااهاااااااهاهااهاااااااالاااهاااالااهااهاااااااااالالااااالااهاهههاهاخاااااللهاهاااااااااااالاایااااااااهلااااااااایااالاااللاعههععهااااهااااااهلاااااالاعااعااااهلاااعلاالعااعاخلااهااااااااااالالااخاالاااهالاااااااهخاهخاالاااالاهااااالاخلهعالالااههااااخالایاالکالاااهاااخالایااااااااااعااالالاااخلالاللعاالهالاااااییاخاخلاالاهااهااااااهعااخلالاعهایاااااححاااکهااایاهااااهاااعلاااههااااااههیهاااااااااهاهاااااااهایخااخئس
@arunjoseph662
@arunjoseph662 2 жыл бұрын
​@@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.
@hichaam2
@hichaam2 2 жыл бұрын
@@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.
@johnfriend2010
@johnfriend2010 Жыл бұрын
excelente explicacion 👌👌
@souparnodey7382
@souparnodey7382 2 жыл бұрын
amazing video
@streettrialsandstuff
@streettrialsandstuff 2 жыл бұрын
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
@ranganrangan5633
@ranganrangan5633 2 жыл бұрын
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?
@yanxu6184
@yanxu6184 Жыл бұрын
Cool,Thanks!Where are there flow for excample code?
@AndroidDevelopers
@AndroidDevelopers Жыл бұрын
Sure! You can check out our samples: goo.gle/3A5BvtJ or the new Now In Android app: goo.gle/3SxsIHU 😎
@technics6215
@technics6215 Жыл бұрын
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.
@jonneymendoza
@jonneymendoza Жыл бұрын
That's were u use your own imagination and know how's
@technics6215
@technics6215 Жыл бұрын
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
@jonneymendoza
@jonneymendoza Жыл бұрын
@@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
@schinsky6833
@schinsky6833 2 жыл бұрын
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?
@omkarpawar1741
@omkarpawar1741 Жыл бұрын
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 ?
@luuvanthao3679
@luuvanthao3679 Жыл бұрын
thanks
@MicheleFerretti93
@MicheleFerretti93 2 жыл бұрын
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.
@JoseAlcerreca
@JoseAlcerreca 2 жыл бұрын
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.
@SimoneLeoni
@SimoneLeoni 2 жыл бұрын
@@JoseAlcerreca That is a great answer
@rinkup2639
@rinkup2639 Жыл бұрын
Amazing
@heshansandeepa9471
@heshansandeepa9471 8 ай бұрын
awesome
@dev_jeongdaeri
@dev_jeongdaeri 2 жыл бұрын
👍👍👍👍👍👍
@user-mu9en3tb8v
@user-mu9en3tb8v 3 ай бұрын
cool video)
@flamyoad
@flamyoad 2 жыл бұрын
Need more examples on how to unit test Flows
@pavelgera72
@pavelgera72 Жыл бұрын
благодарю
@gorkemozgur6796
@gorkemozgur6796 2 жыл бұрын
There is also a function called "launchWhenCreated". İsn't that same with "repeatOnLifecycle" ?
@shaiquekhan1185
@shaiquekhan1185 Жыл бұрын
it's very different. launchWhen* functions suspend the coroutine while repeatOnLifeCycle cancels and relaunches the coroutine
@ibrahimheroglu
@ibrahimheroglu 8 ай бұрын
9:14 it should be: ".. as called cold flows"
@Shakesbeer1
@Shakesbeer1 2 жыл бұрын
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?
@vinhnguyenuc4282
@vinhnguyenuc4282 2 жыл бұрын
Good point, still need another solution for "go back from details screen". Livedata does it well
@kirwakelvinkering3122
@kirwakelvinkering3122 11 ай бұрын
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 !
@mahmoudkhairymohamed3572
@mahmoudkhairymohamed3572 Жыл бұрын
WOW
@AndroidDevelopers
@AndroidDevelopers Жыл бұрын
Hello Mahmoud! If you'd like to learn more, please check out this Kotlin playlist: goo.gle/MAD-Kotlin
@chrislagos44
@chrislagos44 2 жыл бұрын
I guess is meaningless to use Flow with Firebase real time database since firebase onDataChange method of a ValueEventListener is always listening for updates.
@MegaBezalel
@MegaBezalel 2 жыл бұрын
15:41 why would ViewModel recreate the flow if ViewModel is not recreated?
@SimoneLeoni
@SimoneLeoni 2 жыл бұрын
Because the activity which is re recreated, calls the collect method
@CanalMateriaGriz
@CanalMateriaGriz 2 жыл бұрын
I wish Flutter uses Kotlin. I love Flutter, but Dart sucks compared to Kotlin...
@wanderer_pranab
@wanderer_pranab 2 жыл бұрын
Hey, I want to build an app, can you teach me, huch much time it would take?
@anmolhira8652
@anmolhira8652 2 жыл бұрын
How do i access Android/data folder and add some .bin files in other app directory. please tell me.
@Ihavetoreturnsomevideotapes
@Ihavetoreturnsomevideotapes 2 жыл бұрын
Why are you guys slowly trying to kill java
@romank5887
@romank5887 2 жыл бұрын
Cause it's old, it sucks and it needs to go away? Or maybe cause Oracle is too greedy?
@siddharthsharma9829
@siddharthsharma9829 2 жыл бұрын
"Pencho" - Virat kohli
@ciarz3r
@ciarz3r 9 ай бұрын
This is very advanced stuff for me. Real hard to understand. :(
@nbits7433
@nbits7433 8 ай бұрын
Poor presentation.
@MrJoeycrackers
@MrJoeycrackers 2 жыл бұрын
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?
@mehdiparsaei1867
@mehdiparsaei1867 2 жыл бұрын
Thanks for sharing! Please add a "{" after " repeatOnLifecycle(Lifecycle.State.STARTED)"
New features in CameraX 1.1, including video and more
15:04
Android Developers
Рет қаралды 12 М.
Kotlin Coroutines 101 - Android Conference Talks
24:49
Android Developers
Рет қаралды 131 М.
Smart Sigma Kid #funny #sigma #comedy
00:19
CRAZY GREAPA
Рет қаралды 9 МЛН
1 класс vs 11 класс  (игрушка)
00:30
БЕРТ
Рет қаралды 3,6 МЛН
Everything you need to know about Kotlin 2.0 🟣
11:05
Stevdza-San
Рет қаралды 47 М.
LiveData with Coroutines and Flow (Android Dev Summit '19)
18:44
Android Developers
Рет қаралды 109 М.
Lazy layouts in Compose
24:32
Android Developers
Рет қаралды 86 М.
KMP vs. Flutter - Who Will Win The Cross-Platform Battle?
16:19
Philipp Lackner
Рет қаралды 26 М.
Understand Kotlin Coroutines on Android (Google I/O'19)
37:49
Android Developers
Рет қаралды 173 М.
Непробиваемый телевизор 🤯
0:23
FATA MORGANA
Рет қаралды 493 М.