adjpd
09/04/2021, 4:41 PMStateFlow
instead of LiveData
. From what I can tell it's main benefits are 1) It's a Kotlin language feature rather than an Android library and 2) you can use intermediate "flow" operations on it rather than use a get()ter method to manipulate the data. That's pretty much it, right?Caio Costa
09/04/2021, 4:54 PMadjpd
09/04/2021, 5:01 PMAlbert Chang
09/04/2021, 5:29 PMLiveData
doesn't work well with Kotlin due to its lack of null safety. This along with the easiness of transforming `StateFlow`s are more than enough to make StateFlow
a better choice. For example, it is extremely verbose to create a derived LiveData
from multiple `LiveData`s, but with `StateFlow`s, it is as simple as using combine
on them.adjpd
09/04/2021, 5:42 PMLefko
09/04/2021, 5:49 PMadjpd
09/04/2021, 5:51 PMhfhbd
09/04/2021, 5:59 PMLefko
09/04/2021, 6:03 PMfun currTime() = System.currentTimeMillis()
fun threadName() = Thread.currentThread().namevar start: Long = 0
fun testBackPressure() {
(1..5)
.asFlow()
.onStart { start = currTime() }
.onEach {
delay(1000)
print("Emit $it (${currTime() - start}ms) ")
}
.collect {
print("\nCollect $it starts (${currTime() - start}ms) ")
delay(3000)
println("Collect $it ends (${currTime() - start}ms) ")
}
}
With flow you get all values. Test it out with LiveData.adjpd
09/04/2021, 6:07 PMonEach
et al as intermediate operators. What else would you call them?Lefko
09/04/2021, 6:09 PMadjpd
09/04/2021, 6:10 PMLefko
09/04/2021, 6:11 PMadjpd
09/04/2021, 6:11 PMenighma
09/05/2021, 4:55 AM