Sangeet
07/08/2025, 6:28 AMcollectAsStateWithLifecycle
on consumer side and stateIn
in producer side to restrict view getting updates
https://medium.com/androiddevelopers/consuming-flows-safely-in-jetpack-compose-cde014d0d5a3
https://medium.com/androiddevelopers/things-to-know-about-flows-sharein-and-statein-operators-20e6ccb2bc74
.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5000L),
null
)
Also there was some suggestion to use it to fetch initial data for a screen rather than using init
or LaunchedEffect(Unit)
as it have its own issue.
One problem which I found was the data will be re-fetched if screen is kept in background for more than 5s which is kind of making bad UX if we don't hold data in repo layer.
Are there any better approach for fetching initial data from compose screens ?Dinoy Raj k
07/08/2025, 6:58 AM