https://kotlinlang.org logo
#flow
Title
# flow
c

chanjungskim

03/24/2023, 2:35 PM
LiveData or StateFlow? I don't understand why people use StateFlow. And there's flow for Repository. I am very confused. Anybody can make me understand them...?
c

CLOVIS

03/24/2023, 2:45 PM
You can't use LiveData server-side, in a JVM Desktop application, in your JS website, or in iOS.
c

chanjungskim

03/24/2023, 4:18 PM
So, LiveData is just for android and StateFlow for other platforms?
That's interesting that server side also uses StateFlow. Does it need to observe data?
c

CLOVIS

03/24/2023, 4:20 PM
StateFlow is not for "other platforms". It's for all platforms, you only need to learn one thing and use it everywhere.
Does the server need to observe data?
Depends on your server. I use flows to cache frequent DB requests in-memory → https://gitlab.com/opensavvy/pedestal/-/blob/main/cache/src/commonMain/kotlin/Cache.kt
Since flows are multiplatform, I can use the same Cache implementation server-side to cache the DB and in the JS code to cache network requests. There's no need anymore to reinvent the wheel on every app.
c

chanjungskim

03/30/2023, 3:06 PM
That's a good point. Thanks for the explanation. 🙂
f

fuad

03/31/2023, 12:48 AM
Also Google mentions it's not necessary to mix LiveData with Flows when using Compose etc...they specifically say using LiveData isn't necessary and just an extra thing. Consuming a Flow from a Composable seems a bit cleaner to me than LiveData as well, and it goes along with the Compose MVI architecture
c

chanjungskim

03/31/2023, 12:49 AM
For me, it's very confusing. It'd be nice if there are commonly used template or something.. I feel like I need to use flow and StateFlow but it's hard to apply..
f

fuad

03/31/2023, 12:52 AM
There are! check out the Google sample projects
10 Views