The task is to send a couple of requests, get data from these requests, process them (data from 2-3 requests) and send to the adapter (for example)

How is it right and beautiful to do? As far as I know, for such tasks RxJava is used, I would like to give an example of the correct solution using Rx and without using Rx.

  • "As far as I know, RxJava is used for such tasks." Who told you such nonsense! AsyncTask, and even better Thread. Sends the same? Sends And the best is the enemy of the good. - SmInc

1 answer 1

If you need to connect the answers of these requests, the operator concat is quite suitable concat Here is a small demonstration

  final String[] aStrings = {"A1", "A2", "A3", "A4"}; final String[] bStrings = {"B1", "B2", "B3"}; final Observable<String> aObservable = Observable.fromArray(aStrings); final Observable<String> bObservable = Observable.fromArray(bStrings); Observable.concat(aObservable, bObservable) .subscribe(getObserver()); 

At the output, we get the data stream of the "A1", "A2", "A3", "A4" and then "B1", "B2", "B3"

You can also use the operator merge

  Observable.merge(aObservable, bObservable) .subscribe(getObserver()); 

then the output is a stream of data

 "A1", "B1", "A2", "A3", "A4", "B2", "B3" 

or zip

 Observable<String> stringObservable1 = Observable.just("Hello", "World"); Observable<String> stringObservable2 = Observable.just("Bye", "Friends"); Observable.zip(stringObservable1, stringObservable2, new BiFunction<String, String, String>() { @Override public String apply(@NonNull String s, @NonNull String s2) throws Exception { return s + " - " + s2; } }).subscribe(new Consumer<String>() { @Override public void accept(String s) throws Exception { System.out.println(s); } }); 

get out

 Hello - Bye World - Friends 

I would not write this purely android threads for this resource I advise you to use rxJava.

  • I would like to know the following, when using android streams, we send the first request, and then the second request) But since we need to combine the data, we must somehow refer to the parameters when these requests have already been processed and returned an answer. And how to implement it concisely? Put a delay? Or how? - Anton
  • @ Anton what is meant by android streams? Ie you do not use rx and use android java streams? ё - elik
  • @ Anton No, rx is the data stream, it will process the first request itself, then send 2 the request will obarbotaet and it will give you ready data of 2x streams in this is its charm) but to do it cleanly and not with java or android it's very low level - elik
  • Here, I would like to get a complete answer in order to compare the hemorrhoid variant with the flows (without px) and the normal variant with Ph - Anton