Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LiveBus同一个key只能添加一个观察者,第二个观察者无法收到通知 #12

Open
0xByteLeon opened this issue Feb 22, 2019 · 0 comments

Comments

@0xByteLeon
Copy link

` public static class LiveBusData extends MutableLiveData {

    private boolean isFirstSubscribe;

    LiveBusData(boolean isFirstSubscribe) {
        this.isFirstSubscribe = isFirstSubscribe;
    }

    @Override
    public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
        super.observe(owner, new ObserverWrapper<>(observer, isFirstSubscribe));
    }
}

private static class ObserverWrapper<T> implements Observer<T> {

    private Observer<T> observer;

    private boolean isChanged;

    private ObserverWrapper(Observer<T> observer, boolean isFirstSubscribe) {
        this.observer = observer;
        isChanged = isFirstSubscribe;
    }

    @Override
    public void onChanged(@Nullable T t) {
        if (isChanged) {
            if (observer != null) {
                observer.onChanged(t);
            }
        } else {
            isChanged = true;
        }
    }

}`

问题再isChanged 和 isFirstSubscribe 标志位上,请修改~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant