CaptainDriftwood
CaptainDriftwood

Reputation: 5708

RxJava Scheduler interval http polling wait for request to finish before polling again?

I'm using RxJava with AndroidFastNetworking library. If I wanted to use the Scheduler Class to create an Observable that initialized an Http request every 60 seconds, but I wanted it to wait for the initialized request to finish (either success or error) before starting the 60 second interval again and initializing a subsequent one, what would that look like?

Upvotes: 1

Views: 276

Answers (1)

Dave Moten
Dave Moten

Reputation: 12087

Use a rangeLong source and flatMap with maxConcurrent = 1 (which ensures that flatMap only subscribes to one inner Flowable at a time):

Single<Result> request = ...;
Flowable<Result> delay = 
  Flowable.<Result>empty()
    .delaySubscription(60, TimeUnit.SECONDS, scheduler);
Flowable<Result> flowable = 
  Flowable
    .rangeLong(0, Long.MAX_VALUE)
    .flatMap(n -> request
       .toFlowable()
       .concatWith(delay), 1))
    .doOnNext(result -> System.out.println(result))
    ...   

Upvotes: 0

Related Questions