These are chat archives for ReactiveX/RxJava

29th
Jun 2016
Ivru
@Ivru
Jun 29 2016 14:24
Hi there. Trying a few thing with Rx (android implementation) and I've got a few questions. Let's say I want to use Rx to fetch some data from 2 API call (callA and callB). callB depends on callA. Also, every data from both calls must be store in a DB. After a few try, I've come to the solution with 2 observables : one for callA that will query the API and then store the data using a map transformation, and a second one for callB (that will also store the data using a map). In order to be sure that callA is done before calling callB, I'm using a concatWith on callA.
My question is: is there something more elegant? I was thinking about having one observable per API call (let say call1 and call2) and 1 per storage process (store1 and store2). Thing is, I didn't manage to do so since there isn't any implementation of "Then" operator. Or am I missing the point with Rx ?
Dmitriy Tarasevich
@darussian
Jun 29 2016 15:32
@Ivru have you tried using Observable#flatmap on callA. That's what I usually do if I have a call that's dependent on another.
      Observable<Integer> intObservable = Observable.just("Test")
                .flatMap(new Func1<String, Observable<Integer>>() {
                    @Override
                    public Observable<Integer> call(String s) {
                        return Observable.just(s.length());
                    }
                });
Ben Christensen
@benjchristensen
Jun 29 2016 16:52
FlatMap is the 'then' that supports followup work that is asynchronous. Map is then 'then' for followup work that is just synchronous computational (transformation, no IO).
flatmap is the most important operator to learn and use for composition.