1
votes

I'm new rxjava2.when i read book about it and i have some that i don't understand about operator delay.

We can postpone emissions using the delay() operator. It will hold any received emissions and delay each one for the specified time period. If we wanted to delay emissions by three seconds, we could do it like this:

 public static void main(String[] args) {

    Observable.just("Alpha", "Beta", "Gamma" ,"Delta",
            "Epsilon")
            .delay(3000, TimeUnit.SECONDS)
            .subscribe(s -> System.out.println("Received: " + s));

    sleep(3000);
}

public static void sleep(long millis) {
    try {
        Thread.sleep(millis);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

The output of the preceding code snippet is as follows: Beta Alpha Gamma Delta Epsilon

I think that output is only "Alpha", because they said

Because delay() operates on a different scheduler (such as Observable.interval()), we need to leverage a sleep() method to keep the application alive long enough to see this happen. Each emission will be delayed by three seconds

with delay 3s, i think there is a emission as "Alpha",but it emit all of emissions in observable.

3

3 Answers

0
votes

Could this be you wanted ?

val source1 = Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon")

val source2 = Observable.interval(3000, TimeUnit.MILLISECONDS)

val observable = Observable.zip(source1, source2, object : BiFunction<String, Long, Any> {
    override fun apply(t1: String, t2: Long): Any {
        Log.d("Sometag", "Received $t1")
        return "Something"
    }
}).subscribe()

Output

2019-02-21 13:40:15.502  D/Sometag: Received Alpha
2019-02-21 13:40:18.502  D/Sometag: Received Beta
2019-02-21 13:40:21.502  D/Sometag: Received Gamma
2019-02-21 13:40:24.502  D/Sometag: Received Delta
2019-02-21 13:40:27.502  D/Sometag: Received Epsilon
0
votes

According to Documentation, Delay operator 'delay' emittions within given time.

So, it will display 'Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon', not only 'Alpha'.

println("started")

val subscribe = Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
        .delay(3, TimeUnit.SECONDS)
        .subscribe { s -> println("Received: $s") }

this code will produce all five strings emitted after 3s.

  • 2019-02-21 18:02:30.285 I: started
  • 2019-02-21 18:02:33.459 I: Received: Alpha
  • 2019-02-21 18:02:33.464 I: Received: Beta
  • 2019-02-21 18:02:33.466 I: Received: Gamma
  • 2019-02-21 18:02:33.467 I: Received: Delta
  • 2019-02-21 18:02:33.469 I: Received: Epsilon
0
votes

in your case the delay operation will only will delay all stream 3 second and all element will be emitted immediately se more in rx documentation http://reactivex.io/documentation/operators/delay.html

if you want to delay every element by 3 second you can do it like this :

       Observable.fromArray("Alpha", "Beta", "Gamma", "Delta",
            "Epsilon")
            .concatMap(s -> Observable.just(s).delay(3, TimeUnit.SECONDS))
            .subscribe(s -> System.out.println("Received: " + s));