I'm playing around with RXJava, retrofit in Android. I'm trying to accomplish the following:
I need to poll periodically a call that give me a Observable> (From here I could did it)
Once I get this list I want to iterate in each Delivery and call another methods that will give me the ETA (so just more info) I want to attach this new info into the delivery and give back the full list with the extra information attached to each item.
I know how to do that without rxjava once I get the list, but I would like to practice.
This is my code so far:
pollDeliveries = Observable.interval(POLLING_INTERVAL, TimeUnit.SECONDS, Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR))
.map(tick -> RestClient.getInstance().getApiService().getDeliveries())
.doOnError(err -> Log.e("MPB", "Error retrieving messages" + err))
.retry()
.subscribe(deliveries -> {
MainApp.getEventBus().postSticky(deliveries);
});
This is giving me a list of deliveries. Now I would like to accomplish the second part.
Hope I been enough clear. Thanks
.flatMap
receives a Location object (this could be aList<Something>
), then inside.flatMap
the Location is transformed intoList<Address>
, finally in.map
the list of addresses is transformed into a string. If.map
was excluded from the example, then.subscribe
would have receivedList<Address>
. So to sum up the events, the observer initial receives a Location, which is then transformed:Location -> flatMap(Location) -> List<Address> -> map(List<Address>) -> String -> subscribe(String)
. – Abstract.flatMap()
the Delivery into ETA (so you iterate over the output in reactive style), and thentoList()
at the end so that the final output is an observable publishing a list of ETAs? – Ludendorff