:bulb: The benefits of collecting view-state `Stat...
# compose-desktop
d
💡 The benefits of collecting view-state
StateFlow
s using the new API
collectAsStateWithLifecycle
on Android are well explained in this article. alphabet yellow question For those of us developing for K *Compose/Multiplatform*; what's the most ergonomic way to achieve
StateFlow
collection that has the benefits of
collectAsStateWithLifecycle
on Android, while being able to reuse (mostly) the same View code on other platforms?
h
The android ViewModel and its lifecycle is very specific to android. One option, use expect/actual and use system functions to detect the current focus/background state to throttle your flows/logic. For example use the swing windowlistener: https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html
a
Perhaps,
collectAsStateWithLifecycle
will actually save resources in background only when the Flow is cold. In many cases the Flow in a ViewModel is backed by
MutableStateFlow
which is hot. In such cases only re-rendering will be avoided (which might be negligible anyway), the ViewModel will keep working in the background. This is how I understand the picture.
g
I think collectAsStateWithLifecycle (and similar flowWithLifecycle) on practice helpful only for really heavy cases like Location or something like socket connection But those are still pretty important cases