Gorets Gorets - 1 year ago 93
Java Question

Filled model with extra response using Rx

I'm new with Rx java and my current task is I have 2 requests

fun getNews(): Observable<NewsResponse>

fun getNewsImage(@Url url: String): Observable<NewsImage>

and I'd want to add image url to 1st response model from 2nd one. 2nd one depends on 1st one. How is it possible to merge them in Rx stream?

So 1st request returns me list of news, then I need to call
for each item with url from this model, parse real image url from second response, add it to
object and get final list with filled models
Thanks for any advice

Answer Source
        .flatMap { Observable.fromIterable(it.getTopicsList()) } //or Observable.from() if you use RxJava1
        .flatMap({ getNewsImage(it) }, { topic, imageUrl ->
            topic.imageUrl = imageUrl
            return@flatMap topic

Second flatMap combines items from the source Observable with the Observable triggered by those source items, and emits these combinations. More info

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download