1

I have an operation with delay:

mySingle.delaySubscription(60, TimeUnit.SECONDS).subscribe();

If for some reason after scheduling such a delay I want to perform an operation now, how can I do that?

What I see, I can get Disposable from my subscribe(), and, if I need to perform an operation now, dispose it and subscribe again without any delay. But it seems to me as quite rude way.

Is there more elegant way to do that?

Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48
  • Not sure what to try to achieve here, but essentially yes, you can dispose the previous subscription and subscribe to `mSingle` directly next time. Alternatively, you could merge in a `SingleSubject` that let's you react to a value immediately and still get to subscribe to the main source. – akarnokd Sep 26 '18 at 11:19
  • @akarnokd, can you please provide a bit code for the second option? I'm not so experienced in Rx yet – Goltsev Eugene Sep 26 '18 at 11:22

1 Answers1

0

You could merge in a SingleSubject that lets you react to a value immediately and still get to subscribe to the main source.

Single<T> mSingle = ...

SingleSubject<T> immediately = SingleSubject.create();

Flowable<T> result = mSingle
    .delaySubscription(60, TimeUnit.SECONDS)
    .mergeWith(immediately);

result.subscribe();

immediately.onSuccess(...);
akarnokd
  • 69,132
  • 14
  • 157
  • 192