0

I'm using subscription in Apollo with Rxjava2 as follow

Rx2Apollo.from(someApolloCall)
.observeOn(schedulerProvider.io())
.subscribeOn(schedulerProvider.ui())
.subscribe({

  // on success

}, {
  // on failure
})

Everything is working well when the network is ok, however when losing connectivity, subscription fails and it doesn't reconnect again?

how should I reconnect in similar scenarios, I've been trying using rxjava operation retryWhen{} but in vain!

thanks in advance.

Omar Labib
  • 57
  • 7

1 Answers1

0

retryWhen responds onError so as to resubscribe.

The code below retries a limited number of times (3 times and delays each try by 5 counts)

response.retryWhen(errors ->
  errors
    .zipWith(Observable.range(1, 3), (n, i) -> i)
    .flatMap(retryCount -> Observable.timer((long) Math.pow(5, retryCount), TimeUnit.SECONDS))
);

Also, make sure you are subscribing on subscribeOn.Schedulers.io() and observeOn(AndroidSchedulers.mainThread() excutes the emission (actions) on Android main thread.

Ronan
  • 64
  • 1
  • 12
  • 'Rx2Apollo.from(someApolloCall)' is a flowable not observable! should i replace observable in your answer with flowable? – Omar Labib Dec 19 '18 at 09:10
  • Can you provide some code? Not sure what someApolloCall looks like but yes you can replace with Flowable. – Ronan Dec 19 '18 at 09:18
  • can you explain how to write this in kotlin, I've been trying since then buy faces alo of errors – Omar Labib Dec 19 '18 at 09:41