android - RxJava how two sum response of two list called asynchronous? -


i have 2 lists of different objects called using retrofit2. these 2 list work fine , return elements ok. second part both objects have property "importe" (represents money), first need sum objects of each list subtotal , sum subtotal of 2 lists total (totallist1 + totallist2) , show value in textview.

my problem i'm using 2 asynchronous requests can not sum subtotals. code can found here.

i tried use retrofit2 synchronous request error:

android.os.networkonmainthreadexception

so other guys suggested me use rxjava. can explain details how implement in case? not perfect strict syntax tell me e.g.:

//here create rxjava adapter  ...... // here call first request first list<objecta>   //here call second request second list<objectb> ....  //and here subtotal of each list , sum total , put in textview... 

any suggestion , welcome, in advance!

my sdkversion:

minsdkversion 21

targetsdkversion 25

first move out of main thread using operator , subscribing on computation scheduler.

then use flatmap, in flatmap make 2 service calls, , apply flatmap on resulting observables emit list of objects subtotal each list

then use zip total

 observable.just(1).subscribeon(schedulers.computation()).flatmap( dummy -> {      observable<list<objecta>> objectaobservable =  retrofitservice.firstcall().subscribeon(schedulers.io());     observable<list<objectb>> objectbobservable = retrofitservice.secondcall().subscribeon(schedulers.io());      observable<float> subtotala =objectaobservable.flatmap(lst -> {         float subtotal = 0;         for(obj : lst){             subtotal = subtotal + obj.getmoney();         }          return observable.just(subtotal );});       observable<float> subtotalb = objectbobservable.flatmap(lst -> {         float subtotal = 0;         for(obj : lst){             subtotal = subtotal + obj.getmoney();         }          return observable.just(subtotal );});      return observable.zip(subtotala, subtotalb, new bifunction<float, float, string>() {             @override             public string apply(float subtotala, float subtotalb) throws exception {                 return string.valueof(subtotala + subtotala);             }         });     }).observeon(androidschedulers.mainthread())     .subscribe(total -> logger.info("total money - set on textview.. "+total)); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -