I am creating an observable sequence and I am basically looking for a nice way to break the chain if a condition is not met. Ideally we could through an error in this scenario. This is basically to remove an if/else statement in a flatmap operator. This is something like I have now
flatMap(new Func1<User, Observable<Response>>() {
@Override
public Observable<Response> call(Result result) {
if(isValid(result))) {
return api.getObservableResponse(); //some retrofit observable
} else {
observer.onError();
return null; //??? I guess this would force an error
}
}
})
I've seen operators such as filter() and all the other conditional ones but I am not sure if any of them meet my requirements. Is there a better way to do this? Or is what I have looking fine? Thanks!
How about using takeWhile
- docs?
Observable<T> whatever = ...
Observable<T> untilConditionMet = whatever
.takeWhile(this::isValid) // modify according wherever isValid comes from
.flatMap(r -> api.getObservableResponse()); // only doing this until isValid
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With