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
- if data item code matches item code (from outer loop)
- if item of specific type
- 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:
- you can convert , normal data structures various observable operators.
fromiterable()
,just()
convert observables, whiletolist()
converts list. - use
map()
when operation converts 1 value or 1 non-observable another; useflatmap()
when need convert observable values produces.
Comments
Post a Comment