I want to emmit one item after x seconds if no items have been emited. I was trying to use the timeout operator. The problem is that timeout operator requires at least one item to be executed previously to start the countdown. From the documentation:
"If the next item isn't emitted within the specified timeout duration starting from its predecessor, the resulting Observable begins instead to mirror a fallback Observable."
That is not the behaviour I'm looking for. When I subscribe to my observable, I want to emit a particular item if a particular period of time elapses without any emitted items previously.
Example:
getUserLocationFromGPS() //Sometimes I dont receive user location
.timeout(5, TimeUnit.SECONDS, Observable.just(getDefaultLocation())
.subscribe(...);
final Observable data = yourApi.getData().take(5, TimeUnit.SECONDS);
final Observable fallback = Observable.just("Fallback");
Observable.concat(data, fallback).firstOrError()
.subscribe(
System.out::println
);
If yourApi.getData()
does not emit anything then you will receive data from your fallback observable.
If yourApi.getData()
timeouts then you will receive data from your fallback observable.
If yourApi.getData()
emits normally then you will receive data from it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With