Joe Rakhimov
Joe Rakhimov

Reputation: 5083

RxJava - What is the difference between delay and delaySubscription?

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?

Upvotes: 13

Views: 10346

Answers (3)

okipol
okipol

Reputation: 1219

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.

Upvotes: -1

y.allam
y.allam

Reputation: 1516

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

Upvotes: 35

Kiskae
Kiskae

Reputation: 25603

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

Upvotes: 6

Related Questions