rx java - How could I simplify this multiple dependent calls with RxJava2 -


i'd know how perform following rxjava2,

  • make api call list of items
  • iterate on list of items
    • if item of specific type
      • make api call user details
      • make api call list of data item id & user name (from details)
      • iterate on list of data
        • if data item code matches item code (from outer loop)
          • update item in outer list copying data item it
  • return list

am pretty new rxjava , tried doing single initial api call , using map operator, , in mapper function did rest of stuff normal outer , inner loop.

i'd love know if mapper function part somehow done rxjava in simpler way rather using nested loops?

your outer loop might this:

observable.fromiterable( list )   .flatmap( item -> checkitemandreplace( item ) )   .tolist(); 

and inner loop be:

observable<itemtype> checkitemandreplace( itemtype item ) {   if ( isitemofspecifictype( item ) ) {     return getupdatesforitem( item );   }   return observable.just( item ); } 

and on inner loop. broke things down nested function calls returning observables, can recombine them observable chain, depending on code style , testing needs.

the points note:

  1. you can convert , normal data structures various observable operators. fromiterable() , just() convert observables, while tolist() converts list.
  2. use map() when operation converts 1 value or 1 non-observable another; use flatmap() when need convert observable values produces.

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -