Stanislav Kral
04/26/2024, 8:18 PMrepeatOnLifecycle
in an activity like so?
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppUi()
}
lifecycleScope.launch {
repeatOnLifecycle(state = Lifecycle.State.CREATED) {
userIdFlow.collect { userId ->
log.d("userId: $userId")
updateAppCenterUserId(userId)
}
}
}
}
}
Looking at some logs from production environment I've seen cases where the userId
is logged several times in a row, looking as if userIdFlow.collect
was called multiple times. Right before aforementioned logs it seems like there is around ~15 minutes of inactivity in the app.
userIdFlow
is initiated by MutableStateFlow<String?>(null)
There might be an issue that several file logging Timber trees were planted, but other log calls don't seem to be repeated this way.Atul Gupta
04/28/2024, 8:29 AMrepeatOnLifecycle(state = Lifecycle.State.CREATED) {
and userIdFlow.collect { userId ->
getting called. code snippet seems correct. It should only register when lifecycle moved to CREATED
state and should deregister when lifecycle moves to DESTROYED
sateAnselmo Alexandre
04/29/2024, 10:26 AMSTARTED
and cancel when the lifecycle is STOPPED
.