4
votes

I have just started learning RxJava and thinking reactive. I have found some article (7 tips) that says subjects should only be used as a last resort or the first implementation, and I have been thinking.. As of now I have one PublishSubject

   PublishSubject.create()
    .scan(0, (a, b) -> a + 1).subscribe(replaySubject)

It is incrementing the value by 1 every time, and is subscribed by replaySubject

ReplaySubject.create();

That just prints the value. So at start I have 0, then on every PublishSubject.onNext I am incrementing the value, so I got 1, 2 etc. Because its replaySubject I am getting the whole chain. However I have been thinking whether or not this may be done without subjects? I have been reading the RxJava Wiki and the operators, but I can't figure out any way this could be done.

Update

So the pseudo code I am trying to archieve is somehow to have one observable that starts with 1

Observable.just(1)

Now I do have on click listener

OnClick{ }

And every time I click on some button I want to see all previous numbers + 1, so 1 then 1, 2 next 1,2,3 etc. I have been trying with ConnectableObservator.replay however this does not succeeded at all. And inside listener I jave been trying to first add scan on my Observable to increase value and then subscribe so I can print it. But this does not work either. Damn I think i'm in a black corner and misunderstood the idea of observables...

1
You want to replay the items of PublishSubject? - zsxwing
I want to eliminate PublishSubject and ReplaySubject, as I could somewhow increment my value from 0 to x on lets say mouse click - user3274539
Why observable.scan(0, (a, b) -> a + 1) dost not work? - zsxwing
It does, but lets say I have Observable.just(0) and a button click event, now I have one subscriber that subscribes to my Observable and prints its value. So it prints 0. Now I on every button click I want to increment that value and send it to all subscribers, I might not understand the whole idea yet - user3274539
Do you have an Observable for the button click events? Maybe you can write some pseudo code to help us understand your case. - zsxwing

1 Answers

6
votes

Since you are writing an Android app, you can use RxAndroid. Here is an example,

    Observable.just(1).flatMap(new Func1<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> call(Integer initialValue) {
            return ViewObservable.clicks(button, false)
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .scan(initialValue, new Func2<Integer, View, Integer>() {

                        @Override
                        public Integer call(Integer integer, View v) {
                            return integer + 1;
                        }
                    });
        }
    }).subscribe(new Observer<Integer>() {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onNext(Integer integer) {
            System.out.println(integer);
        }
    });

I'm not sure if it's what you want. Maybe you only need:

    ViewObservable.clicks(button, false)
            .subscribeOn(AndroidSchedulers.mainThread())
            .scan(1, new Func2<Integer, View, Integer>() {

                @Override
                public Integer call(Integer integer, View v) {
                    return integer + 1;
                }
            }).subscribe(new Observer<Integer>() {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onNext(Integer integer) {
            System.out.println(integer);
        }
    });