I have AsyncTask , which I want to remake in Rx

Here is my AsyncTask

 new AsyncTask<Void, Void, Void>() { ArrayList<CallServiceCode> undoneServiceCodes = new ArrayList<>(); HashMap<String, ServicePartFormQuestionsManager> undoneForms = new HashMap<>(); boolean isHasAtLeastOneDoneServiceCode = false; @Override protected void onPreExecute() { super.onPreExecute(); if (iCloseCallListener != null) { iCloseCallListener.onPreValidation(); } } @Override protected Void doInBackground(Void... params) { undoneServiceCodes = getUndoneServiceCodes(); undoneForms = getUndoneForms(); isHasAtLeastOneDoneServiceCode = isHasAtLeastOneDoneServiceCode(); return null; } @Override protected void onPostExecute(Void iVoid) { super.onPostExecute(iVoid); if (iCloseCallListener != null) { iCloseCallListener.onPostValidation(undoneServiceCodes, undoneForms, isHasAtLeastOneDoneServiceCode); } } }.execute(); 

I do not understand how you can perform in the Rx background, you can perform 3 different methods at once, as in the example with AsyncTask when 3 methods are running in the background

  1. undoneServiceCodes = getUndoneServiceCodes ();
  2. undoneForms = getUndoneForms ();
  3. isHasAtLeastOneDoneServiceCode = isHasAtLeastOneDoneServiceCode ();

If this would be one method (let's say the first), I would do it

 Flowable.fromIterable(getUndoneServiceCodes())// .subscribeOn(Schedulers.io())// .observeOn(AndroidSchedulers.mainThread())// .doOnSubscribe(iSubscription -> { if (iCloseCallListener != null) { iCloseCallListener.onPreValidation(); } }).toList()// .subscribe(resultList -> { if (iCloseCallListener != null) { iCloseCallListener.onPostValidation(iCloseCallListener, ???, ???); } }); 

But so I will get the result for only one method running in the background, how to do it so that you can process 3?

  • @ YuriySPb added - Aleksey Timoshchenko

1 answer 1

You can use the zip operator somehow (the accuracy of method names is not guaranteed):

 Flowable.zip( Flowable.fromCallable(method1()), Flowable.fromCallable(method2()), Flowable.fromCallable(method3()), (result1, result2, result3) -> new Triple(result1, result2, result3) ) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(triple -> iCloseCallListener.onPostValidation(triple.first, triple.second, triple.third)) 
  • Yes, really what you need, thank you. It’s just not quite convenient that this Triple class is essentially a pojo object, so this object takes up a bunch of lines ... Of course, if you use kolin, there’s no problem at all) - Aleksey Timoshchenko
  • @AlekseyTimoshchenko, yes, with a Kotlin is easier - there this class enters into the standard lib) If you can put Java into the array, then you don’t need to create extra classes. Although this, of course, will not be very beautiful) - YuriySPb