I have a problem with a label value being skipped ...
# mvikotlin
p
I have a problem with a label value being skipped if using coroutines extension
labels
when the label is published immediately on store startup. I’ve modified my previous sample to illustrate the issue. I expect to see “a” in the first text and “bb” in the second text.
AStore
dispatches an
Action.PublishLabel
in the bootstrapper, and the action handler publishes
Label.LabelPublished
. I subscribe to
labels
in the init block of
DefaultWelcomeComponent
but the label event is never received. But if you add a
delay(<long>)
call before the
publish
call, the label event is received. I initially thought this had something to do with the order of initialization of all the objects, but it seems that wasn’t the case, since if you use
labels(observer: Observer<Label>): Disposable
the label event is still received. How would you suggest to deal with this?
a
Yep. You have to either publish the label asynchronously (e.g. by calling
yield
from a coroutine before publishing), or initialise (bootstrap) the store manually. For the latter, see "Initializing a Store" here: https://arkivanov.github.io/MVIKotlin/store.html
p
Thanks, and when should
Store.init()
be called in this example?
a
Right after all subscriptions and bindings are performed.
p
Well, I’ve tried creating it with
autoInit = false
, and added the
store.init()
here, but nothing changed
a
I think you need to use Dispatchers.Main.immediate
p
in debugger I see 0 observers in labelSubject
a
Otherwise you're subscribing asynchronously
p
Oh, thanks! It actually works with
Dispatchers.Main.immediate
even with
autoInit = true
🎉 1
a
Awesome!