google/agera

[Question] A problem about using repository

zjutkz opened this issue · 5 comments

Hi,when I was reading chapter "Activation lifecycle and event chain",I know that observable can register two or more updatables,so I tried to register two updatables onto a sample observable which just notify these two updatables.And when I change my code to let the second updatable do a delay registration using handler.postDelay(Runnable r, long delayMillis),it also works and the second updatable receive the event later.
But when I using repository to do the same thing,the second updatable cannot receive the event when I tried to make it delay.I read the source code of agera and found that BaseObservable's function addUpdatable(final Updatable updatable) will send message MSG_FIRST_ADDED when the variable size just equals 1,it means if someone register two updatables and make the second one delay,it cannot receive the events.(But if we register it immediately after registering the first one,it works because using handler send message MSG_FIRST_ADDED is a time consuming operation)
So is there any workaround?

When having multiple Updatables it's important to remember that the Observable is active from the addition of the first Updatable until the removal of the last one. If an update() is triggered before the Updatable is added it might never get an update() if no additional is sent. If you require an init of your Updatable you would have to self-update by calling updatable.update() just after your call to observable.addUpdatable(updatable).

Being a common usage pattern we're actually discussing turning this into a single operation (like an addAndUpdate, or similar). +@maxtroy for additional thoughts on this.

@ernstsson Thanks for ur help,it helps me a lot.

@ernstsson When I using observable in my code,both two updatables will receive the event even the second updatable is delay registered,because I'm not call any removal functions so the observable is active.But repository does not work,the second updatable cannot receive the event if I using handler to do a delay registration.So I wonder is there any workaround to fit this issue—The updatable cannot receive the event if I make it delay registration when I using repository as an observable
Like this:

repository.addUpdatable(updatable_one);
hanlder.postDelayed(new Runnable() {
            @Override
            public void run() {
                repository.addUpdatable(updatable_two);
            }
        },1000);

The updatable_two will not receive the trigger.

It is the same situation. If this repository you're using does not send out any more updates after 1000ms after being activated, the updatable_two will not get any calls.

What is that repository and how did you change its value / make it broadcast updates?

@maxtroy Okay,it seems clearer to me,I know how repository works.Thanks a lot!