RXjava为对象更新实现了一个observable

时间:2017-01-17 21:09:43

标签: java rx-java

如何将一个observable添加到Integer对象,以便我收到每个对象更新的通知?在这种情况下,整数= 0;我想在每次添加例子时收到通知1.最初我只是想记录事件。这就是我到目前为止所做的,但是我被卡住了。

代码:

Subscriber<Integer> num = new Subscriber<Integer>() {
        @Override
        public void onNext(Integer num) { Log.d("RX", num.toString()); }

        @Override
        public void onError(Throwable e) { Log.d("RX", "error"); }

        @Override
        public void onCompleted() { Log.d("RX", "completed"); }
    };

    Observable.just(number)
            /*.doOnNext(new Action1<Integer>() {
                @Override
                public void call(Integer integer) {
                    Log.d("RX", "Updated integer" + integer.toString());
                }
            })*/
            /*.flatMap(new Func1<Integer, Observable<?>>() {
                @Override
                public Observable<?> call(Integer integer) {
                    Log.d("RX", "Updated integer" + integer.toString());
                    return Observable.just(number);
                }
            })*/
            .subscribe(num);

2 个答案:

答案 0 :(得分:4)

如果不确切知道自己要做什么,很难给出最佳答案,但您可以按照以下方式实施ObservableInteger课程:

public class ObservableInteger {

    private Integer value;
    private PublishSubject<Integer> subject = PublishSubject.create();

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
        subject.onNext(value);
    }

    public Observable<Integer> getObservable() {
        return subject.asObservable();
    }
}

然后您可以订阅它:

    ObservableInteger obInt = new ObservableInteger();

    Subscription s = obInt
            .getObservable()
            .subscribe(
                    integer -> {
                        // each time a new value is set
                        // it will be emitted here
                    },
                    Throwable::printStackTrace
            );

答案 1 :(得分:1)

我今天遇到了这篇文章,并意识到它是为RxJava1编写的。有几个changes in RxJava 2。其中一些影响上述示例。 (.subscribe返回Disposable https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#subscription,而RxJava 2.0 https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#instance-methods中的.asObservable()现在是.hide())

这是Jahnolds答案的RxJava2兼容解决方案:

    public class ObservableInteger {

        private Integer value;
        private PublishSubject<Integer> subject = PublishSubject.create();

        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
            subject.onNext(value);
        }

        public Observable<Integer> getObservable() {
            return subject.hide();
        }
    }

并这样订阅:

    ObservableInteger obInt = new ObservableInteger();

    Disposable d = obInt
            .getObservable()
            .subscribe(
                    integer -> {
                        // each time a new value is set
                        // it will be emitted here
                    },
                    Throwable::printStackTrace
            );