StateFlow vs. Flow vs. SharedFlow vs. LiveData... When to Use What?! - Android Studio Tutorial

preview_player
Показать описание
In this video you'll learn the differences between the typical observable classes we have in Android.

⭐ Get certificates for your future job
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:

💻 Let me personally review your code and provide individual feedback, so it won't backfire and cost you a fortune in future:

Subscribe to my newsletter for regular Android, Kotlin & Architecture advice!

Join this channel to get access to perks:

Regular live codings on Twitch:

Join my Discord server:

You like my free content? Here you can buy me a coffee:

00:00 - Introduction
01:46 - LiveData
04:30 - StateFlow
08:11 - Flow
10:58 - SharedFlow
16:30 - Compose state
Рекомендации по теме
Комментарии
Автор

1. LiveData is an lifecycle aware obervable data holder ( means it knows the lifecycle of the activity or an fragment) use it when you play with UI elements(views).
2. StateFlow(hot stream) does similar things like LiveData but it is made using flow by kotlin guys and only difference compare to LiveData is its not lifecycle aware but this is also been solved using repeatOnLifecycle api's, So whatever LiveData can do StateFlow can do much better with power of flow's api.
3. SharedFlow(hot stream) - name itself says it is shared, this flow can be shared by multiple consumers, I mean if multiple collect calls happening on the sharedflow there will be a single flow which will get shared across all the consumers unlike normal flow.
4. Flow (cold stream) - In general think of it like a stream of data flowing in a pipe with both ends having a producer and consumer running on a coroutine.

bjugdbjk
Автор

Big mistake here: Neither LiveData nor StateFlow keep the state alive on activity re-creation/rotation. That's the job of the ViewModel.
I'm not trying to nitpick here, but you've mentioned it several times (2:20, 6:22) and it's definitely a mistake.
The purpose is LiveData/StateFlow is to call you back when the value changes, instead of you repeatedly checking if changes happened.
This is called the observer pattern, where LiveData/StateFlow is the observable (holding the value you are interested in) and your activity is the observer (someone who is interested in changes)
Again, if you put LiveData/StateFlow into the activity or fragment instead of the ViewModel, then the state will be lost on screen-rotation.

suauhwang
Автор

That's what i want recently. it's legendary.
you're a magician. thank you for everything you give the subscribers all the videos!!

keepgoingman
Автор

The best yet simplest explaination. Covers the use cases and differences both! Thanks a ton Philipp.

nishant_thite
Автор

As someone who is new to reactive programming, this video is quite useful. LiveData, Flow, StateFlow, SharedFlow, Compose states, it all makes sense now. I'll check out another concept (Channel) and then proceed to one of your MVVM projects 😊

DennisEspiritu
Автор

googled hours didn't found what I was looking for. 5 minutes into you video and I figured it out. Thanks mate! Love your Videos!

MrDonSocke
Автор

I watched lots of YouTube videos and doc but i can't understand the different. But your video is more clearly explained..thanks for the video.

ashokmanis
Автор

This video is brilliant.

I decided to rebuild our company app using Kotlin and Jetpack Compose. You really helped me get my head around things.

Thank you very much!

Shanetim
Автор

This was a fantastic explanation and comparison. Thank you Philipp

SamRamezanli
Автор

Thanks a lot for this! I had surrounded myself with Android Jetpack libraries and wanted to check out Flow. I'll definitely try out more of SharedFlow

pankajkhushalani
Автор

The video I was waiting for. Thank you 💕

louismusole
Автор

you are the best teacher in the world, i really hope that you continue recording video for you channel very often, i really thank to you. xD

vadymhrynyk
Автор

Really helped me a lot as your all videos do ❤️
Thx man and keep sharing your quality content more and more you are the best 👏

mohamedayad
Автор

You deserve more subs.. one of the best android channel

iftak
Автор

Great Video Philipp, really explained a lot, thanks

flowzk
Автор

Thanks for the very important topic you covered. Do you have any video where you showed how to use StateFlow or SharedFlow with Retrofit api call along with state handle using Kotlin sealed class?
If not, please (if possible) make a video on CRUD by showing how to combine all those things using MVVM pattern.

shohagkhan
Автор

Nice explanations dude! Your video has helped me a lot and thanks to everybody in the comments for errata.

shinyparadise
Автор

Awesome content ❤️. Can't thank you enough for your hard work

tareksellami
Автор

One important feature of SharedFlow is that it will emit the same value, and StateFlow won't.

samstoyroom
Автор

You make real Topics man... keep making keep growing...😀😀

codingcosmos