Kotlin Flows in practice

Поделиться
HTML-код
  • Опубликовано: 10 фев 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;

Комментарии • 87

  • @karthikss6501
    @karthikss6501 3 года назад +20

    Wow. Its really amazing to see coroutine has evolved in different level now a days. Kudos to the team🙌🏻

  • @MuhammadFarhan-pq2wc
    @MuhammadFarhan-pq2wc 3 года назад +22

    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 Год назад +1

      Wait till you hook up state flows with composable screens. match made in heaven

  • @HaSeebpjr01
    @HaSeebpjr01 Год назад +1

    best toturial ever i watch about Flows

  • @jeffreynyauke7636
    @jeffreynyauke7636 3 года назад +9

    Nice video. You should cover Flows using Jetpack Compose

  • @thecsciworker291
    @thecsciworker291 3 года назад +2

    Wow. Really took a concept and broke it down. Thank you for this!

  • @vengateshm2122
    @vengateshm2122 3 года назад +5

    Excellent content. Good to see that life cycle related pain points are addressed elegantly.

  • @theredd1703
    @theredd1703 Год назад +1

    Love it the interactive way you did the video!

  • @deepakbisht4957
    @deepakbisht4957 3 года назад +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...

  • @hichaam2
    @hichaam2 3 года назад +16

    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 3 года назад +6

      @@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 3 года назад +6

      @@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 3 года назад

      تگنتتلتتتببببببتخبلتتبتتتتتتتتهتهبتتتختختا/88 ج/یالان إثخاأاآخااآآخااهاالتاالیتکیاثااتاتااالآاآحتلیآاالااعاتاتااااهاعاااعاااااتاااالهااعاخااالااااهاااااااالاعحاااالاتااهاااااااااخاخاتاااااااههااااخااهخهاهااختااخاااهااااهخااالااالاااالااااااااااهخااااالاهلهااهااااالاااااالالتاااااهاااالهااااتمخاتاهاعتاااااخاااهاااالالللاخخاهالخااهالاااهاهااااخلاعاتهلااههااخاااااخالختهااااالاهاااالاالاااهاااااللاالاااااخهااااااااالخهاهااهااااااااااااهااااااااااهاهلااااااااااخالااااالاااهااایاهاااااااااااخاخااهاالاااخاالاااهلعاهاهالااالاااااهالاااللاااااهلاااهااخااااالاهاالاااهاااااااهاهااهاااااااالاااهاااالااهااهاااااااااالالااااالااهاهههاهاخاااااللهاهاااااااااااالاایااااااااهلااااااااایااالاااللاعههععهااااهااااااهلاااااالاعااعااااهلاااعلاالعااعاخلااهااااااااااالالااخاالاااهالاااااااهخاهخاالاااالاهااااالاخلهعالالااههااااخالایاالکالاااهاااخالایااااااااااعااالالاااخلالاللعاالهالاااااییاخاخلاالاهااهااااااهعااخلالاعهایاااااححاااکهااایاهااااهاااعلاااههااااااههیهاااااااااهاهاااااااهایخااخئس

    • @arunjoseph662
      @arunjoseph662 3 года назад

      ​@@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 3 года назад +1

      @@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.

  • @xeon3325
    @xeon3325 2 года назад +1

    Very vivid explanation of the article

  • @dithyramb6152
    @dithyramb6152 2 года назад +2

    Thank you! You've helped me solve a lot of problems!

    • @AndroidDevelopers
      @AndroidDevelopers  2 года назад +1

      You are so welcome 😀
      We're pleased to hear this has helped you on your Android journey 🛤️

  • @thiagolopessilva
    @thiagolopessilva 2 года назад +1

    Excellent content. Thanks to share it with us and now I can understand flow in Android context.

  • @kreutzerzetanism7396
    @kreutzerzetanism7396 Год назад +1

    this seems so seamless

  • @shriharshabhagwat6289
    @shriharshabhagwat6289 6 месяцев назад

    Best video on Flows on android!

  • @goobar
    @goobar 3 года назад +1

    Great work with this one guys!!

  • @witoldsienski1709
    @witoldsienski1709 3 года назад +3

    Finally an alternative to rx java :)

  • @Mrdresden
    @Mrdresden 2 года назад +3

    Very informative, thanks!

    • @AndroidDevelopers
      @AndroidDevelopers  2 года назад +1

      Delighted to hear this! Have you checked out the documentation for Kotlin Flows on Android?: goo.gle/3AXlXFi
      Happy learning! 😎

  • @chuka_obi5167
    @chuka_obi5167 3 года назад +1

    This was an awesome tutorial on flows! Thank you

  • @anteckoeros
    @anteckoeros 3 года назад +2

    Yeah nice video and Pancho is the best example, thank you :)

  • @megaxenu753
    @megaxenu753 Год назад +1

    surprisingly this video actually helped.

  • @nima7605
    @nima7605 2 года назад +1

    Very helpful with an excellent explanation

  • @hoonjung2143
    @hoonjung2143 3 года назад +2

    Muchas gracias! Good stuff and well explained! Keep on the good work. Thank you :)

  • @Padpaad
    @Padpaad 3 года назад +1

    nice presentation about the flow!!

  • @navczydev
    @navczydev 3 года назад +2

    Great talk thank you folks

  • @ILikeActions
    @ILikeActions 3 года назад +1

    Amazing content, thank you very much to provide material that it so easy to be understanded!

  • @schinsky6833
    @schinsky6833 2 года назад +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?

    • @idonnoe
      @idonnoe 3 месяца назад

      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.

  • @technics6215
    @technics6215 2 года назад +1

    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

  • @sureshmiyani722
    @sureshmiyani722 2 года назад +1

    you can do what you want!

  • @jeckonly5853
    @jeckonly5853 3 года назад +1

    nice example!! You the handsome guy really help me a lot

  • @iandamping9757
    @iandamping9757 3 года назад +1

    Great Video!

  • @yanxu6184
    @yanxu6184 2 года назад +2

    Cool,Thanks!Where are there flow for excample code?

    • @AndroidDevelopers
      @AndroidDevelopers  2 года назад

      Sure! You can check out our samples: goo.gle/3A5BvtJ or the new Now In Android app: goo.gle/3SxsIHU 😎

  • @streettrialsandstuff
    @streettrialsandstuff 3 года назад +1

    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

  • @johnfriend2010
    @johnfriend2010 Год назад

    excelente explicacion 👌👌

  • @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 ?

  • @MicheleFerretti93
    @MicheleFerretti93 3 года назад +4

    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 3 года назад +14

      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 3 года назад

      @@JoseAlcerreca That is a great answer

  • @souparnodey7382
    @souparnodey7382 3 года назад

    amazing video

  • @MargieBrown-z3q
    @MargieBrown-z3q 10 месяцев назад

    cool video)

  • @ranganrangan5633
    @ranganrangan5633 3 года назад

    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?

  • @ibrahimheroglu
    @ibrahimheroglu Год назад

    9:14
    it should be:
    ".. as called cold flows"

  • @Shakenbeer
    @Shakenbeer 3 года назад +1

    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 3 года назад +2

      Good point, still need another solution for "go back from details screen". Livedata does it well

    • @kirwakelvinkering3122
      @kirwakelvinkering3122 Год назад

      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 !

  • @flamyoad
    @flamyoad 3 года назад +3

    Need more examples on how to unit test Flows

  • @CanalMateriaGriz
    @CanalMateriaGriz 2 года назад +1

    I wish Flutter uses Kotlin.
    I love Flutter, but Dart sucks compared to Kotlin...

  • @rinkup2639
    @rinkup2639 2 года назад

    Amazing

  • @luuvanthao3679
    @luuvanthao3679 Год назад

    thanks

  • @MegaBezalel
    @MegaBezalel 3 года назад

    15:41 why would ViewModel recreate the flow if ViewModel is not recreated?

    • @SimoneLeoni
      @SimoneLeoni 3 года назад +1

      Because the activity which is re recreated, calls the collect method

  • @dev_jeongdaeri
    @dev_jeongdaeri 3 года назад

    👍👍👍👍👍👍

  • @heshansandeepa9471
    @heshansandeepa9471 Год назад

    awesome

  • @gandautama4141
    @gandautama4141 2 месяца назад

    lifesaver

  • @gorkemozgur6796
    @gorkemozgur6796 3 года назад

    There is also a function called "launchWhenCreated". İsn't that same with "repeatOnLifecycle" ?

    • @shaiquekhan1185
      @shaiquekhan1185 2 года назад

      it's very different. launchWhen* functions suspend the coroutine while repeatOnLifeCycle cancels and relaunches the coroutine

  • @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.

  • @pavelgera72
    @pavelgera72 2 года назад

    благодарю

  • @anmolhira8652
    @anmolhira8652 3 года назад

    How do i access Android/data folder and add some .bin files in other app directory. please tell me.

  • @mahmoudkhairymohamed3572
    @mahmoudkhairymohamed3572 2 года назад

    WOW

    • @AndroidDevelopers
      @AndroidDevelopers  2 года назад

      Hello Mahmoud! If you'd like to learn more, please check out this Kotlin playlist: goo.gle/MAD-Kotlin

  • @wanderer_pranab
    @wanderer_pranab 3 года назад

    Hey, I want to build an app, can you teach me, huch much time it would take?

  • @siddharthsharma9829
    @siddharthsharma9829 3 года назад +2

    "Pencho" - Virat kohli

  • @ciarz3r
    @ciarz3r Год назад +1

    This is very advanced stuff for me. Real hard to understand. :(

  • @SynthfulDuck
    @SynthfulDuck 5 месяцев назад +1

    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.

  • @nbits7433
    @nbits7433 Год назад

    Poor presentation.

  • @MrJoeycrackers
    @MrJoeycrackers 3 года назад +1

    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 3 года назад +3

    Thanks for sharing!
    Please add a "{" after " repeatOnLifecycle(Lifecycle.State.STARTED)"