Answer the question
In order to leave comments, you need to log in
Rx: how to repeat a subscription until a certain result?
Hello!
I can’t formulate the question exactly, I’ll describe what is needed:
Yes Single<SomeObject>
- a request through Retrofit.
I SomeObject
have a field status
And I need to repeat this request until this field takes a certain value.
And only after that subscribe
should get the result.
Something like repeatWhen
, but in repeatWhen I cannot get the returned SomeObject to check the status (as I understand it from the documentation).
Is there a way to do this elegantly, purely on Rx?
Thanks in advance!
Answer the question
In order to leave comments, you need to log in
You can use switchMap recursively.
If the result is satisfactory, then return it wrapped in just. Otherwise, return a new Single by pulling the same function.
Consider a delay.
Of course, I'm not an Rx master, but I came across this article on Habré Understanding retryWhen and repeatWhen
A piece of code from the article that applies to your problem
public void load() {
Observable.combineLatest(
repository.getSomething()
.retryWhen(retryHandler ->
retryHandler.flatMap(
err -> retrySubject.asObservable())),
localStorage.fetchSomethingReallyHuge()
.retryWhen(retryHandler ->
retryHandler.flatMap(
nothing -> retrySubject.asObservable())),
(something, hugeObject) -> new Stuff(something, hugeObject))
.subscribe(stuff -> {}, err -> {});
}
retryWhen(
) is executed only after an error inside the source, respectively, if only one of the sources “errors”, then the resubscription will occur only to it, and the remaining chain below will wait for reexecution. retryHandler
will work in two places.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question