Colton Idle
06/26/2024, 11:42 AMfun userIdFlow(): Flow<UserId> = userFlow.distinctUntilChangedBy { it?.it }.onEach { analyticLog(it) }.shareIn(viewModelScope, Lazily)
and my composable screen has
LaunchedEffect(Unit){
viewModel.userIdFlow().collect()
}
I'm probably doing something completely wrong. but im still a flow noob so appreciate pointing out any glaring issue here. thanks!Albert Chang
06/26/2024, 12:03 PMshareIn
or stateIn
, always make it a member instead of a function, otherwise every time you call the function a new flow will be created, which essentially makes shareIn
or stateIn
useless.Colton Idle
06/26/2024, 2:28 PMAlbert Chang
06/26/2024, 3:10 PMonEach
operator is applied before shareIn
, and once the sharing starts it won’t stop (because of Lazily
), so it’ll just work as you expect (i.e. onEach
runs once for each value) if you make it a class member.Zach Klippenstein (he/him) [MOD]
06/26/2024, 4:14 PMColton Idle
06/26/2024, 5:03 PM