调用doOnSubscribe时,RxJava2 PublishSubject没有观察者

时间:2017-12-25 17:14:36

标签: android publish-subscribe rx-java2

我正在使用RxJava2。

我有一些可观察的,很少有订阅者可以订阅它。

每次新用户到达时,都应该完成一些工作,并且应该通知每个订户。 为此,我决定使用PublishSubject。但是当从第一个订阅者收到doOnSubscribe时,myPublishSubject.hasObservers()返回false ...

知道为什么会发生这种情况,我该如何解决这个问题?

    private val myPublishSubject = PublishSubject.create<Boolean>()

    fun getPublishObservable():Observable<Boolean> {
        return myPublishSubject.doOnSubscribe { 
            //do some job when new subscriber arrived and notify all subscribers
            //via 
            myPublishSubject.onNext(true)
        }
    }

我是否理解这一点,当doOnSubscribe调用它意味着至少有一个订户已经存在?

1 个答案:

答案 0 :(得分:0)

我没有找到准备好的答案,所以我创建了自己的主题版本,并将其命名为 RefreshSubject

它基于PublishSubject,但有一点不同:如果您想返回observable并在新订阅者到达并准备接收某些数据时收到通知,您应该使用方法getSubscriberReady。 这是一个小例子:

private RefreshSubject<Boolean> refreshSubject = RefreshSubject.create();


//ordinary publish behavior
public Observable<Boolean> getObservable(){
    return refreshSubject;
}

//refreshSubject behaviour
public Observable<Boolean> getRefreshObserver(){
    return refreshSubject.getSubscriberReady(new Action() {
        @Override
        public void run() throws Exception {
            //new subscriber arrives and ready to receive some data
            //so you can make some data request and all your subscribers (with new one just arrived)
            //will receive new content
        }
    });
}

这是全班:

public class RefreshSubject<T> extends Subject<T> {
    /** The terminated indicator for the subscribers array. */
    @SuppressWarnings("rawtypes")
    private static final RefreshSubject.RefreshDisposable[] TERMINATED = new RefreshSubject.RefreshDisposable[0];
    /** An empty subscribers array to avoid allocating it all the time. */
    @SuppressWarnings("rawtypes")
    private static final RefreshSubject.RefreshDisposable[] EMPTY = new RefreshSubject.RefreshDisposable[0];

    /** The array of currently subscribed subscribers. */
    private final AtomicReference<RefreshDisposable<T>[]> subscribers;

    /** The error, write before terminating and read after checking subscribers. */
    Throwable error;

    /**
     * Constructs a RefreshSubject.
     * @param <T> the value type
     * @return the new RefreshSubject
     */
    @CheckReturnValue
    public static <T> RefreshSubject<T> create() {
        return new RefreshSubject<T>();
    }

    /**
     * Constructs a RefreshSubject.
     * @since 2.0
     */
    @SuppressWarnings("unchecked")
    private RefreshSubject() {
        subscribers = new AtomicReference<RefreshSubject.RefreshDisposable<T>[]>(EMPTY);
    }

    @Override
    public void subscribeActual(Observer<? super T> t) {
        RefreshSubject.RefreshDisposable<T> ps = new RefreshSubject.RefreshDisposable<T>(t, RefreshSubject.this);
        t.onSubscribe(ps);
        if (add(ps)) {
            // if cancellation happened while a successful add, the remove() didn't work
            // so we need to do it again
            if (ps.isDisposed()) {
                remove(ps);
            }
        } else {
            Throwable ex = error;
            if (ex != null) {
                t.onError(ex);
            } else {
                t.onComplete();
            }
        }
    }


    public Observable<T> getSubscriberReady(final Action onReady){
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(ObservableEmitter<T> e) throws Exception {
                add(new RefreshDisposable(e, RefreshSubject.this));
                onReady.run();
            }
        });
    }

    /**
     * Tries to add the given subscriber to the subscribers array atomically
     * or returns false if the subject has terminated.
     * @param ps the subscriber to add
     * @return true if successful, false if the subject has terminated
     */
    private boolean add(RefreshSubject.RefreshDisposable<T> ps) {
        for (;;) {
            RefreshSubject.RefreshDisposable<T>[] a = subscribers.get();
            if (a == TERMINATED) {
                return false;
            }

            int n = a.length;
            @SuppressWarnings("unchecked")
            RefreshSubject.RefreshDisposable<T>[] b = new RefreshSubject.RefreshDisposable[n + 1];
            System.arraycopy(a, 0, b, 0, n);
            b[n] = ps;

            if (subscribers.compareAndSet(a, b)) {
                return true;
            }
        }
    }

    /**
     * Atomically removes the given subscriber if it is subscribed to the subject.
     * @param ps the subject to remove
     */
    @SuppressWarnings("unchecked")
    private void remove(RefreshSubject.RefreshDisposable<T> ps) {
        for (;;) {
            RefreshSubject.RefreshDisposable<T>[] a = subscribers.get();
            if (a == TERMINATED || a == EMPTY) {
                return;
            }

            int n = a.length;
            int j = -1;
            for (int i = 0; i < n; i++) {
                if (a[i] == ps) {
                    j = i;
                    break;
                }
            }

            if (j < 0) {
                return;
            }

            RefreshSubject.RefreshDisposable<T>[] b;

            if (n == 1) {
                b = EMPTY;
            } else {
                b = new RefreshSubject.RefreshDisposable[n - 1];
                System.arraycopy(a, 0, b, 0, j);
                System.arraycopy(a, j + 1, b, j, n - j - 1);
            }
            if (subscribers.compareAndSet(a, b)) {
                return;
            }
        }
    }

    @Override
    public void onSubscribe(Disposable s) {
        if (subscribers.get() == TERMINATED) {
            s.dispose();
        }
    }

    @Override
    public void onNext(T t) {
        if (subscribers.get() == TERMINATED) {
            return;
        }
        if (t == null) {
            onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
            return;
        }
        for (RefreshSubject.RefreshDisposable<T> s : subscribers.get()) {
            s.onNext(t);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onError(Throwable t) {
        if (subscribers.get() == TERMINATED) {
            RxJavaPlugins.onError(t);
            return;
        }
        if (t == null) {
            t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
        }
        error = t;

        for (RefreshSubject.RefreshDisposable<T> s : subscribers.getAndSet(TERMINATED)) {
            s.onError(t);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onComplete() {
        if (subscribers.get() == TERMINATED) {
            return;
        }
        for (RefreshSubject.RefreshDisposable<T> s : subscribers.getAndSet(TERMINATED)) {
            s.onComplete();
        }
    }

    @Override
    public boolean hasObservers() {
        return subscribers.get().length != 0;
    }

    @Override
    public Throwable getThrowable() {
        if (subscribers.get() == TERMINATED) {
            return error;
        }
        return null;
    }

    @Override
    public boolean hasThrowable() {
        return subscribers.get() == TERMINATED && error != null;
    }

    @Override
    public boolean hasComplete() {
        return subscribers.get() == TERMINATED && error == null;
    }

    /**
     * Wraps the actualEmitter subscriber, tracks its requests and makes cancellation
     * to remove itself from the current subscribers array.
     *
     * @param <T> the value type
     */
    private static final class RefreshDisposable<T> extends AtomicBoolean implements Disposable {

        private static final long serialVersionUID = 3562861878281475070L;
        /** The actualEmitter subscriber. */
        final Emitter<? super T> actualEmitter;

        /** The actualEmitter subscriber. */
        final Observer<? super T> actualObserver;
        /** The subject state. */
        final RefreshSubject<T> parent;

        /**
         * Constructs a PublishSubscriber, wraps the actualEmitter subscriber and the state.
         * @param actualEmitter the actualEmitter subscriber
         * @param parent the parent RefreshProcessor
         */
        RefreshDisposable(Emitter<? super T> actualEmitter, RefreshSubject<T> parent) {
            this.actualEmitter = actualEmitter;
            this.parent = parent;
            actualObserver = null;
        }
        /**
         * Constructs a PublishSubscriber, wraps the actualEmitter subscriber and the state.
         * @param actualObserver the actualObserver subscriber
         * @param parent the parent RefreshProcessor
         */
        RefreshDisposable(Observer<? super T> actualObserver, RefreshSubject<T> parent) {
            this.actualObserver = actualObserver;
            this.parent = parent;
            actualEmitter = null;
        }

        public void onNext(T t) {
            if (!get()) {
                if (actualEmitter != null)
                    actualEmitter.onNext(t);

                if (actualObserver != null)
                    actualObserver.onNext(t);
            }
        }

        public void onError(Throwable t) {
            if (get()) {
                RxJavaPlugins.onError(t);
            } else {
                if (actualEmitter != null)
                    actualEmitter.onError(t);

                if (actualObserver != null)
                    actualObserver.onError(t);
            }
        }

        public void onComplete() {
            if (!get()) {
                if (actualEmitter != null)
                    actualEmitter.onComplete();

                if (actualObserver != null)
                    actualObserver.onComplete();
            }
        }

        @Override
        public void dispose() {
            if (compareAndSet(false, true)) {
                parent.remove(this);
            }
        }

        @Override
        public boolean isDisposed() {
            return get();
        }
    }
}