These are chat archives for ReactiveX/RxJava

13th
Nov 2018
Ari Meidan
@AriMeidan
Nov 13 2018 11:13
Hi, i need some help.
I need to take an array of integers that each represent time before calling a method.
I've tryied to create an Observable from Array, but i dont know how to make the delay for the onNext() to be called.
David Karnok
@akarnokd
Nov 13 2018 12:36
@AriMeidan flatMap them onto a timer:
Observable.fromArray(100, 200, 500, 1000)
.flatMap(t -> Observable.timer(t, TimeUnit.MILLISECONDS).map(v -> t))
.blockingSubscribe(v -> System.out.println("Signal T=" + v));
Ari Meidan
@AriMeidan
Nov 13 2018 12:39
@akarnokd Whats the internal map for?
Ari Meidan
@AriMeidan
Nov 13 2018 12:53
@akarnokd you need to use this .flatMap(t -> Observable.timer(t, TimeUnit.MILLISECONDS).map(v -> t), 1) to limit the number of concurency.
David Karnok
@akarnokd
Nov 13 2018 13:45
So you don't just get a bunch of zeros.
If you want a delay between items then yes.
Ari Meidan
@AriMeidan
Nov 13 2018 13:45
its just mapping to itself. Why zeros?
Ari Meidan
@AriMeidan
Nov 13 2018 13:49
Oh, i get it. so i'll get the t that was passes.
*passed
David Karnok
@akarnokd
Nov 13 2018 13:50
You can edit comments.
Ari Meidan
@AriMeidan
Nov 13 2018 13:50
I ignore it because i just need the trigger to call a method.
David Karnok
@akarnokd
Nov 13 2018 13:51
Yes, adapt it to your needs.
Ari Meidan
@AriMeidan
Nov 13 2018 13:55

Now i've changed it so when onNext is called i emit the next value from the array. I need to show an infinite loop of screens each showen for a different time.

    private Observable<Integer> screenTimeSteam() {
        return Observable.create(emitter -> {
            StreamFinishEventListener finishListener = new StreamFinishEventListener(){
                int i = 0;
                @Override
                public void onFinished() {
                    int item = i % timers.length;
                    i++;
                    emitter.onNext(timers[item]);
                }
            };
            setFinishListener(finishListener);
            finishListener.onFinished();
        });
    }//screenTimeSteam

Is there an easier way?

David Karnok
@akarnokd
Nov 13 2018 14:08
How about doing the flatMap I suggested with a repeat after fromArray?