0

So I have my form model which holds all data I want to validate and then send to the server. Let's keep it as simple as possible - isFormValid or api request should return Observable.errr(throwable) which should call onError() in the subscriber.

public void submitForm(Form form){
    Observable
    .just(form)
    .flatMap(form->{
        if(isFormValid(form))
            return Observable.just(form);
        else
            return Observable.error(someValidationError);
    })
    .flatMap(form->{
        Request req = new Request(form);
        try{
            return Observable.just(getResponseFrom(req));
        }
        catch(ApiException e){
            return Observable.error(e)
        }
    }).subscribe(
        new Subscriber<ResponseModel>(){
            @Override
            public void onComplete(){}
            @Override
            public void onError(Throwable t){}
            @Override
            public void onNext(ResponseModel model){}
        }
    );
}

Ok, now let's say user enters invalid data, submitForm() is called and -sure enought- onError is called in subscriber and then onComplete. The user then enters valid data and submitForm() is called again.

Now here's the problem - in the second submitForm() call nothing happens! At least flatMap Func1 and the second flatMap Func2 are not called. Why? What am I doing wrong ? Is it an architectural flaw?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
aldorain
  • 790
  • 5
  • 16
  • Not sure why you need `flatMap`, it looks like `map` would work the same here. – OneCricketeer Mar 15 '16 at 17:41
  • 1
    Because I want to have an ability to raise a proper error via `Observable.error()`. You think throwing an exception would be better? Would it still result in calling `onError`? – aldorain Mar 15 '16 at 17:43
  • You are taking a single Observable from `just`, so `flatMap` simply isn't the correct operation. Do you just want to "discard" some invalid forms and throw an error and emit the rest? – OneCricketeer Mar 15 '16 at 17:47
  • 1
    There's nothing wrong with your code. It should work as you expect. Are you sure `submitForm` is called the second time? – Egor Neliuba Mar 15 '16 at 19:23
  • @cricket_007 in a word yes, the only difference is that I will emit always one `form` (whenever `submitForm()` is called). @Egor yes, I'm also tried unsubscribing althought it should not be required as `onError` is followed by `onCompleted` – aldorain Mar 15 '16 at 21:16
  • So I played around with a simple Rx stream, and any errors that occur will end the Observable at the `onComplete` method. Maybe that is what you are experiencing when the second call doesn't happen. – OneCricketeer Mar 15 '16 at 21:25
  • Ok figured out what was wrong. In my project I tried to use the same instance of `Subscriber` instead of creating a new one (like in the example). Switched to `Observer` and everything works fine now. Also got rid of `flatMaps` replacing them with `map` and using `throw Exceptions.propagate` instead. Cheers! – aldorain Mar 16 '16 at 16:25

0 Answers0