Skip to content

Instantly share code, notes, and snippets.

@David-Hackro
Last active August 8, 2018 02:11
Show Gist options
  • Save David-Hackro/6ec1b7915f910ba500a14b2953e8ed63 to your computer and use it in GitHub Desktop.
Save David-Hackro/6ec1b7915f910ba500a14b2953e8ed63 to your computer and use it in GitHub Desktop.
int COUNTER_START = 1;
int ATTEMPTST = 7;
int ORIGINAL_DELAY_IN_SECONDS = 2;
@Override
public Observable<ResponseService> getAllBus(String idSearch) {
return remoteData.servicePolling(idSearch)
.repeatWhen(new Func1<Observable<? extends Void>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Void> observable) {
return observable.flatMap(new Func1<Void, Observable<?>>() {
@Override
public Observable<?> call(Void aVoid) {
if(COUNTER_START > ATTEMPTST){ //Verificamos que no se sobrepase el numero de intentos
throw new RuntimeException();
}
COUNTER_START++;
return Observable.timer(ORIGINAL_DELAY_IN_SECONDS, TimeUnit.SECONDS); //Indicamos que la siguiente peticion se hara a los n segundos
}
});
}
}).takeUntil(new Func1<ResponseService, Boolean>() {
@Override
public Boolean call(ResponseService ResponseService) {
return ResponseService.getState().equals("finished");//Verificamos el status de la bandera
}
}).distinct();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment