0
votes

My requirement is like this.

  1. Hit the First API get the response. In the response, I have a field defining the status, if that flag is set, make another API call and merge with the first result.
  2. If the flag is not set, return the actual response(First Response).

If I use the filter, it will ignore the false items, if the flag is not set, I don't want to ignore the current response.

How can we achieve this using RxJava, FlatMap and Filter ?

2
Why marking as invalid ? give me a hint ?? - sreekumar

2 Answers

2
votes

You don't need filter. Just check the status in flatMap. If it's set, fire the request and call startWith to concat it with the original one (You can replace startWith with any codes to combine the results as you want); if not, use just to wrap the response. Here is an example:

class MyResponse {

    boolean status;

    public MyResponse(boolean status) {
        this.status = status;
    }

    public static Observable<MyResponse> request() {
        return Observable.just(new MyResponse(false));
    }
}

Observable<MyResponse> response = Observable.just(new MyResponse(false));

response.first().flatMap(r -> {
        if (r.status) {
            return MyResponse.request().startWith(r);
        } else {
            return Observable.just(r);
        }
});
0
votes

Just use filter

  RxHelper.toObservable(request1)
         .filter(response1 -> response1.status == "200")
         .flatMap(response1-> RxHelper.toObservable(request2))
         .map(response2 -> //Bla)
  });