THIS Flow Mistake Will Lead to Bugs in Your Code (StateFlow & Reactive Programming)

  Рет қаралды 28,962

Philipp Lackner

Philipp Lackner

Күн бұрын

Flows are an awesome framework for reactive programming. However, to get the most out of them you have to understand the core concepts of reactivity. In this video I'll talk about some common mistakes.
⭐ Get certificates for your future job
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:
pl-coding.com/...
💻 Let me be your mentor and become an industry-ready Android developer in 10 weeks:
pl-coding.com/...
Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!
pl-coding.com/...
Join this channel to get access to perks:
/ @philipplackner
Join my Discord server:
/ discord
Regular programming advice on my Instagram page: / _philipplackner_
Checkout my GitHub: github.com/phi...
You like my free content? Here you can buy me a coffee:
www.buymeacoff...

Пікірлер: 84
@OlafAchthoven
@OlafAchthoven 2 жыл бұрын
You actually showed the perfect reason to name the lambda parameters. Should be a good practice to not work with 'it'.
@s-w
@s-w 2 жыл бұрын
Yep, but I still use "it" when it's clear and there are no nested "its".
@ChrisAthanas
@ChrisAthanas 2 жыл бұрын
Phillipp, it would be helpful to us if you turned on `inlay hints` so we can see the types of your variable as opposed to having to manually select "type info"
@skarloti
@skarloti 2 жыл бұрын
mutableStateListOf() instead MutableStateFlow(emptyList()> unless I want to use sealed class ResultOf and get statuses for Loading, Error, Succes. Only in this case would I use Flows.
@PhilippLackner
@PhilippLackner 2 жыл бұрын
1. You can't assume someone is using Compose 2. Even for Compose, I don't recommend using Compose state in a VM directly. Will soon explain in a video why
@skarloti
@skarloti 2 жыл бұрын
@@PhilippLackner class SnapshotStateList : MutableList, StateObject
@ChrisAthanas
@ChrisAthanas 2 жыл бұрын
@@skarloti What is this snippet about?
@skarloti
@skarloti 2 жыл бұрын
@@ChrisAthanas hello, maybe it will become clearer to you visually on the link I am sending you. I hope that's not a problem. The short demo shows how when data in a list is modified, it is updated in the user interface via MVVM. Take a look in row #22: snapshotFlow {} kzbin.info/www/bejne/iIHTiYyurNRlr6s
@abhimanyu.n14
@abhimanyu.n14 2 жыл бұрын
To add on, we can use a lot of methods for derived state in ViewModel like map, combine, filter, reduce, zip, fold, flatmap, etc as per the use-case.
@EvgenSuit
@EvgenSuit 8 ай бұрын
An alternative to using combine and 3 different flows is creating just one, which could be represented by a single data class (wrapped in MutableStateFlow), which holds isLoggedIn, chatMessages and users. With this we can utilize the "update" method and only change the fields that require an update
@eduardoscaranari5262
@eduardoscaranari5262 10 ай бұрын
this is fantastic. thanks for such amazing content Philipp. 🤝
@sabuta100
@sabuta100 2 жыл бұрын
I know Rx is not that popular now that flows are out there, but it would be great if you make a series explaining operators and how to combine them. Your ability to teach and the effort behind that is so incredible that learning such a painful topic as Rx might be, would make it easy to understand. Thanks for all your videos!
@laszloforgacs8210
@laszloforgacs8210 2 жыл бұрын
Very informative, I dig the recent topics on the channel!
@MrVipulLal
@MrVipulLal 2 жыл бұрын
Love your channel
@bofu9662
@bofu9662 2 жыл бұрын
Thanks a lot!
@s-w
@s-w 2 жыл бұрын
Thank you for the tips as usual!
@asadullahilyas
@asadullahilyas 2 жыл бұрын
I was making the same mistake.. I’ll get it fixed tomorrow as soon as I get my hands on that laptop
@ukhanyov
@ukhanyov 2 жыл бұрын
Awesome, thank you
@ashwinpatidar2878
@ashwinpatidar2878 2 жыл бұрын
Thanks you Philipp 👍🔥🤟
@PhilippLackner
@PhilippLackner 2 жыл бұрын
Welcome!
@nguyencodervn
@nguyencodervn 2 жыл бұрын
Thank you
@valtteri_its_james
@valtteri_its_james Жыл бұрын
Mate, you are legend
@ruavaen
@ruavaen 2 жыл бұрын
Great video. Thanks
@norayrmovsisyan
@norayrmovsisyan Жыл бұрын
thank you very much!
@tabassumfatima2398
@tabassumfatima2398 2 жыл бұрын
Best video really like your content 👍
@i_Kode
@i_Kode 2 жыл бұрын
I would recommend passing the timeout as first parameter to SharingStarted.WhileSubscribed(), something like 2 seconds. this will ensure that the flow will not stopped and gets recalculated after screen rotation but instead it will stop if the app is in background.
@foivosstamopoulos4106
@foivosstamopoulos4106 Жыл бұрын
Great tutorial as always!!
@irsathkareem7513
@irsathkareem7513 2 жыл бұрын
Please make video on exoplayer with compose ui CONTROLS, eagerly waiting
@ChrisAthanas
@ChrisAthanas 2 жыл бұрын
He has one, look for Spotify Clone
@quantumgaming7
@quantumgaming7 2 жыл бұрын
Really Nice Tips. Thanks for this.
@ChrisAthanas
@ChrisAthanas 2 жыл бұрын
Phillip, bc there was not a github link to the code, I wrote the code you have here into a project. The first example worked fine, but the second example (with the combine) only accepts the initial state for the chatState StateFlow. All additional `user.update{ it + user }` or `chatMessages.update { it + message }` do not emit additional ChatStates via the `combine`. It looks like this doesn't work as described. You should always run your code in these examples to be sure they are working as intended! It seems that the `combine` flow will not be re-triggered unless the values of the StateFlow variables change. Since the `List` does not change (only the contents of list change), the `combine` is never triggered. You will need to wrap the Lists and add a UUID (any random UUID) that indicates the underlying list has changed in order to trigger the `combine` to re-emit a new value. Thats the only way I could get this example to work.
@PhilippLackner
@PhilippLackner 2 жыл бұрын
I don't get what you mean, I just tried it again and the code is working perfectly fine as intended. A core concept of StateFlow is that it holds a value until it changes, so the collector will not fire until the chat state changes (which is what we want). So in your example, you might've changed the users or chat messages flow's values, but if that change doesn't cause a change in the ChatState (because you might've added a message without adding a user first), your collector will not be triggered. In either way, the combine block will be triggered with every update of the MutableStateFlows (if their value changes).
@ChrisAthanas
@ChrisAthanas 2 жыл бұрын
@@PhilippLackner Im trying to figure out what is going wrong, because the code I have is the same as your code. I am collecting the `chatState` in the MainActivity (just like the first example), but when I call to the viewmodel to add a user or add a new message, (using an `users.update { user }` mechanism ) the `combine` is not triggered. Is there something special about the `collect` that is needed for the `combine`? Maybe you could include a link to your code so I can check out whats going on, because my code is identical to yours and its definitely not updating.
@ChrisAthanas
@ChrisAthanas 2 жыл бұрын
I found a clue, it seems that the `combine` operator is executed on a looper, and if you don't give time for it to execute, you will not see the updates. The issue is that I thought it ran in the same coroutine/thread. When I launched the viewModel functions in a separate coroutine, the updates occurred, but they were batched together, so the collect would only see the latest state, not each individual state change, unlike the first example in the video.
@MyFoenix
@MyFoenix 2 жыл бұрын
Yes, I also tried to change value of my flow in a viewmodel and combine did not fired
@mark-147
@mark-147 2 жыл бұрын
Excellent, thanks. Would be interested in some practical use cases where other SharingStarted values are used.
@Guilo583
@Guilo583 2 жыл бұрын
Thanks for the great tutorial.
@osisuper98
@osisuper98 2 жыл бұрын
You keep giving me reasons to no longer use Compose States. Thank you.
@IoannisAnifantakis
@IoannisAnifantakis 7 ай бұрын
The old good transformation maps :)
@datel666
@datel666 2 жыл бұрын
Great video as always
@omkarpawar1741
@omkarpawar1741 2 жыл бұрын
can you provide a tutorial about mutablestatelistof with jetpack compose
@abdurahmon2827
@abdurahmon2827 Жыл бұрын
Thanks bro you are life saver
@user--10552
@user--10552 8 ай бұрын
thanks a lot.
@imbc7867ghuui9
@imbc7867ghuui9 2 жыл бұрын
Hi bro, please, make a video about the new logcat in AC)
@TheAndroidFactory
@TheAndroidFactory 2 жыл бұрын
Love combine!
@mohammedalramlawi691
@mohammedalramlawi691 2 жыл бұрын
combine is very usefull, I loved it 🤩
@mustafaammar551
@mustafaammar551 2 жыл бұрын
you are the best
@makzimalist
@makzimalist 2 жыл бұрын
As usual, great video, very informativ, well prepared and top quality. Thanks a lot ...!
@GeolseuDeiGamers
@GeolseuDeiGamers 2 жыл бұрын
Awesome!
@radbaver
@radbaver Жыл бұрын
Phillip the G.O.A.T !!!
@LightBueno
@LightBueno Жыл бұрын
legend
@yeyaanshkit
@yeyaanshkit 2 жыл бұрын
Hi Phillip, please create a fresh Series of Android Development for beginners.. Please the Updated one..
@shaiquekhan1185
@shaiquekhan1185 2 жыл бұрын
what's your studio's theme? looks pretty cool.
@GilTheCohen
@GilTheCohen 2 жыл бұрын
Why are you not using mutableStateListOf ?
@summerwise
@summerwise 2 жыл бұрын
Hello, could you make a video about navigation with TopBar? I mean how to change topBar title and back button after changing our screen. In xml we can bind our toolbar to navgraph, idk how to do that on jetpack compose in right way.
@MyFoenix
@MyFoenix 2 жыл бұрын
Also, as mentioned above, I tried to change flow value in a viewmodel and combine did not fired. It was only fires when items goes from repository for example
@cularu1
@cularu1 2 жыл бұрын
why don't you just filter the list? instead of map and find?
@icycleragon
@icycleragon 2 жыл бұрын
Filter returns a list and he wanted a specific user
@PhilippLackner
@PhilippLackner 2 жыл бұрын
Filter would filter the flow, not the list. So with filter you could say that some list emissions are completely dropped
@AmitJayant
@AmitJayant Жыл бұрын
In the combine example, isLoggedIn, chatMessages & users have default values. They will never change. How to make them get data from database?
@carlosmspk
@carlosmspk Жыл бұрын
in that last example, I don't understand why you say "Else we'd have to recalculate everything when any of the parameters changed". Isn't that what's happening anyways? Isn't there a way to also get WHICH of the combined parameters changed? We might be talking about a very long list, and I don't want to recompute the entire thing if another, unrelated variable changed which would only change the variable in an easy to compute way (imagine, it'd only change a color, not the value of the list, or something similar)
@PhilippLackner
@PhilippLackner Жыл бұрын
The problem you mentioned is what I was meaning. There's no way to detect that except for comparing the fields old and new value. You'd need to make each field a separate state flow then to detect that
@carlosmspk
@carlosmspk Жыл бұрын
@@PhilippLackner Hah, I thought you meant that "combine" somehow solved that problem, and wasn't really understanding why. Thanks for clearing that up!
@samstoyroom
@samstoyroom 2 жыл бұрын
What's the difference between map{} and mapLatest{} ?
@stanleykou5643
@stanleykou5643 2 жыл бұрын
Wow
@MyFoenix
@MyFoenix 2 жыл бұрын
Hi! Did you try to combine more then 5 values? When I tried I got an error about types...
@PhilippLackner
@PhilippLackner 2 жыл бұрын
at some point, you'll get an array of items since they don't want to create a million overloads of course
@MyFoenix
@MyFoenix 2 жыл бұрын
@@PhilippLackner but in sorces varargs is used.... and this is popular case, complicated screens can use more then 5 flows...
@PhilippLackner
@PhilippLackner 2 жыл бұрын
@@MyFoenix yea but you get it as array
@Khaled_Ahmed_Elsayed
@Khaled_Ahmed_Elsayed 2 жыл бұрын
But this isn't related entirely to flows, you are just stating the single responsibility principal, and avoiding coupling 2 variables in 1 function, isn't it?
@PhilippLackner
@PhilippLackner 2 жыл бұрын
No, this is not really related to SRP 😅 The video is about using flows correctly to avoid bugs and race conditions
@pinoy_marites_beh
@pinoy_marites_beh 2 жыл бұрын
I tried exactly what you did in your example @Philipp, but it only updates the second flow (localUser) once, and every time I do something on the "_users" parent flow, the "localUser" flow doesnt get updated..
@PhilippLackner
@PhilippLackner 2 жыл бұрын
It's only updated if the result (the local user) changes
@pinoy_marites_beh
@pinoy_marites_beh 2 жыл бұрын
@@PhilippLackner ohh, I see, I thought the premise was any updates "happened" on the "_user" , it will then automatically reflect (updates) to the "localUser"
@PhilippLackner
@PhilippLackner 2 жыл бұрын
@@pinoy_marites_beh add a println statement in the map block and you should see that it gets updated :) but the collector of localUser will only be called if the value changes
@pinoy_marites_beh
@pinoy_marites_beh 2 жыл бұрын
@@PhilippLackner Still wrapping my head around when and where should I expect it to be updated though, maybe I'm thinking a different use-case, but anway, thank you Philipp, Great content as always.
@mdisi5967
@mdisi5967 2 жыл бұрын
Does live data have anything like this 🤔
@PhilippLackner
@PhilippLackner 2 жыл бұрын
Yes, there are some transformations, but i haven't used it in a while. It's not as much as flows offer, which is also not the job of live data though (it's a state holder and not a framework for reactive programming)
@tsmusic9587
@tsmusic9587 Жыл бұрын
u so handsome :*
@alicjamruk8446
@alicjamruk8446 2 жыл бұрын
I really like your videos, they are very informative, but could you please be aware of using 'actually', it happens too often.
@PhilippLackner
@PhilippLackner 2 жыл бұрын
I know I'm actually already trying 😂
@fereshtemohamadi9237
@fereshtemohamadi9237 2 жыл бұрын
Hi, Im fan of you from IRAN. People of IRAN need to theire voice heared. Please check the name #mahsa amini to figour out. Please be voice of iranians
@original_anu
@original_anu 2 жыл бұрын
This could be a good hint by IDE. When you are using state.value in operators like combine, suggest using that state in the dependents list Similar useEffect with useState in react.
@pyaesonehan19
@pyaesonehan19 9 күн бұрын