I’m having an initialization issue with this code ...
# coroutines
j
I’m having an initialization issue with this code :
Copy code
lifecycleScope.launchWhenCreated {
            viewModel.state
                .onEach { updateState(it) }
                .onStart { inputsCollector.loadData() }
                .launchIn(this)
inputsCollector
emit values on a channel when
loadData
is called, the values are then used by a state machine. the problem here is that
inputsCollector.loadData()
is called too soon, before the function in the state machine collects the values (I guess) even though I call that function before launching the coroutine. If I had
delay(100)
before
inputsCollector.loadData()
everything works fine. Any idea how when I should called
inputsCollector.loadData()
?
looks like using
lifeCycleScope.launch(Dispatchers.Main
fixed the issue