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
hfhbd
02/23/2023, 8:20 AM
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
Arkadii Ivanov
02/23/2023, 9:16 AM
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
gildor
02/23/2023, 9:42 AM
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