4

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(...);
2
  • maybe just start with getDefaultLocation()? Commented Oct 3, 2017 at 10:03
  • @KrzysztofSkowronek I don't want to emit the default location at first, it is only the fallback case. Commented Oct 3, 2017 at 10:23

2 Answers 2

4
final Observable data = yourApi.getData().take(5, TimeUnit.SECONDS);
final Observable fallback = Observable.just("Fallback");

Observable.concat(data, fallback).firstOrError()
            .subscribe(
                    System.out::println
            );
  1. If yourApi.getData() does not emit anything then you will receive data from your fallback observable.

  2. If yourApi.getData() timeouts then you will receive data from your fallback observable.

  3. If yourApi.getData() emits normally then you will receive data from it.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I am already using that parameter. Mi problem is not that. You are always emitting one item, "Hello". In my case, I could have no items emitted previously, so the timeout operator does not start the countdown.
@VictorManuelPinedaMurcia Can you mark the answer as accepted for clarity purpose then? Glad to help!
0

I don't use java-rx specifically, but here is an idea:

getUserLocationFromGPS()
.buffer(5 sec, 1 item) //not sure how to do this in rx-java
.map(x => { // this is select in .net
    if(x.IsEmpty)
      return getDefautLocation();
    else
      return x[0];
    }).Subscribe(...)

buffer will get you list of items every 5sec. You can specify it to return after only 1 item was emited by getUserLocationFromGPS()

map will check if the list is empty (no item was emited) and return proper value

1 Comment

I'm afraid it not working for me. If getUserLocationFromGPS() doesn't emit an item, buffer not emit a empty list after 5 secs. Map operator it not executed in that case. I will stick to Benjamin solution. Thanks you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.