danieln
danieln

Reputation: 4973

Nested method calls with Observable return value

I have a workflow I'm trying to achieve using RxJava, as I'm pretty new with it I'm having a hard time understanding how to implement it. This is what I'm trying to achieve:

    Observable<String> methodA(String input){
       retValue = methodB(input);
       if(something){
         return retValue;
       } else{
         return methodC(input);
       }       
    }

    Observable<String> methodB(String input);
    Observable<String> methodC(String input);

I'm trying to use map() but actually I can't find a way that actually works.

Any thoughts?

Upvotes: 1

Views: 502

Answers (2)

paul
paul

Reputation: 13471

Or you can use combination filter/switchIfEmpty if you want avoid old fashion if

  retValue = methodB(input)
            .filter(something)
            .switchIfEmpty(methodC(item))

Upvotes: 1

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

Use .flatMap() (if your condition depends on the results of methodB):

   retValue = methodB(input)
   .flatMap(item -> {
     if(something){
       return just(item);
     } else{
       return methodC(item);
     }
   })

Upvotes: 1

Related Questions