RxJava - What is the difference between delay and delaySubscription?
Asked Answered
S

3

12

I am trying to understand the difference between delay and delaySubscription operators.

This documentation describes delay operator:

The Delay operator modifies its source Observable by pausing for a particular increment of time (that you specify) before emitting each of the source Observable’s items. This has the effect of shifting the entire sequence of items emitted by the Observable forward in time by that specified increment

enter image description here

delaySubscription is described like this:

There is also an operator with which you can delay the subscription to the source Observable: delaySubscription.

enter image description here

However, when I tested the behaviour of these 2 operators, it seems to me they are working in the same way.

This is my observable:

Observable observable = Observable.just(5, 3, 4, 2);

Here I am using delay operator:

observable = observable.delay(3, TimeUnit.SECONDS);

This is my observer which logs when one of its methods called

Observer observer = new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d("RxJavaTag", "onSubscribe");
            }

            @Override
            public void onNext(Object o) {
                Log.d("RxJavaTag", "onNext: " + o);
            }

            @Override
            public void onError(Throwable e) {
                Log.d("RxJavaTag", "onError:" + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d("RxJavaTag", "onComplete");
            }
        };

Here Observer is subscribing to Observable:

observable.subscribe(observer);

In result, I am getting the following log when I am using delay operator:

03-25 17:45:34.156 onSubscribe
03-25 17:45:37.160 onNext: 5
03-25 17:45:37.160 onNext: 3
03-25 17:45:37.160 onNext: 4
03-25 17:45:37.160 onNext: 2
03-25 17:45:37.160 onComplete

And the following log when using delaySubscription operator:

03-25 17:49:22.540 onSubscribe
03-25 17:49:25.544 onNext: 5
03-25 17:49:25.544 onNext: 3
03-25 17:49:25.544 onNext: 4
03-25 17:49:25.544 onNext: 2
03-25 17:49:25.544 onComplete

As you can see in logs, they are working in the same way. I could not understand the difference between them. I tried to test it with ConnectableObservable - the same behaviour.

In my understanding and as its name suggests, should not delaySubscription operator work in this way:

[SOME DELAY - FOR EXAMPLE 3 SECONDS]
03-25 17:49:25.540 onSubscribe
03-25 17:49:25.544 onNext: 5
03-25 17:49:25.544 onNext: 3
03-25 17:49:25.544 onNext: 4
03-25 17:49:25.544 onNext: 2
03-25 17:49:25.544 onComplete

Maybe I am doing something wrong - but I could not understand the difference. Can you please explain the difference between delay and delaySubscription?

Swam answered 25/3, 2018 at 12:59 Comment(0)
C
34

You can imagine this as a stream of a football game, and the source is emitting frames [A hot observable].

Assuming a real-time stream scenario .. In case of delay(3 seconds), once you turn on the channel you'll be subscribed but will receive the first frame after 3 seconds of its actual emission, so you'll continue watching the game with 3 seconds delay.

In case of delaySubscription(3 seconds), once you turn on the channel you'll wait 3 seconds (missing any frames emitted during these 3 seconds) then subscribe and start receiving the rest of the frames in real time

Caffeine answered 25/3, 2018 at 16:5 Comment(1)
That's a very concise explanation. Thanks!Daradarach
R
6

delaySubscription only affects the initial subscription. After it has subscribed to the downstream Observable it operates just as normal.

delay works on the onNext, onComplete and onError events, delaying those for the specified amount of time, but keeping the relative delay between them the same.

The difference would be clearer if you used a less predictable source than Observable.just

Righthand answered 25/3, 2018 at 14:41 Comment(2)
thank you for your answer. You wrote 'delaySubscription only affects the initial subscription'. But as you can see in logs, it is not affecting subscription. What do you mean by 'less predictable source than Observable.just'? It would be great if you set an exampleSwam
Operators that change onSubscribe only do so going up the chain. Since your logging action is in the Observer it is unaffected. If you put the logging action in a doOnSubscribe operation before the delay it should be visible.Righthand
J
-1

It is pretty clear when you look at the operators one beneath the other (like you posted). In general you "loose" the red and orange marble when the subscription is delayed. Compared to delay which will give you those just with an delay.

Juliojulis answered 4/8, 2021 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.