Vivek Sharma02/02/2021, 5:33 PM
jean02/03/2021, 6:54 AM
Orhan Tozan02/03/2021, 2:34 PM
Note: Usingfunctions from the Lifecycle Kotlin extensions to collect a flow from the UI layer is not always safe. When the view goes to the background, the coroutine suspends, leaving the underlying producer active and potentially emitting values that the view doesn't consume. This behavior could waste CPU and memory resources.
*`StateFlow`*s are safe to collect using theA way to combat this is always have a backed shared flow that collects while subscribed.functions since they're scoped to *`ViewModel`*s, making them remain in memory when the
launchWhen()goes to the background, and they do lightweight work by just notifying the
Viewabout UI states. However, the problem might come with other producers that do more intensive work.
jean02/03/2021, 2:50 PM
Orhan Tozan02/03/2021, 2:52 PM
val users: Flow<List<User>> = observeUsers()
val users: Flow<List<User>> = observeUsers() .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())
Vivek Sharma02/03/2021, 3:03 PM
Orhan Tozan02/03/2021, 3:05 PM
Vivek Sharma02/03/2021, 3:09 PM
John O'Reilly02/03/2021, 3:10 PM
jean02/03/2021, 3:31 PM
property on a state/shared flow declared like this
? I see the doc says
val state = Mutable[State/Shared]Flow(initialValue)
the problem might come with other producers that do more intensive workwhat is intensive enough and does it mean it should be changed to a backed shared flow instead?
Vivek Sharma02/03/2021, 3:33 PM
Orhan Tozan02/03/2021, 3:37 PM
jean02/03/2021, 3:39 PM