如何在RxJava中缝制两个Observable?

Observable<List<CalendarEvent>>,对于每个CalendarEvent,我想进行网络操作以读取(纬度,经度)并获取地名,然后将地名合并回CalendarEvent。

 public Observable<List<CalendarEvent>> getEvents() {
     // get events
     // translate each Event LatLng to Place and bind it to Event
     // return the events
 }

最佳答案

public Observable<List<CalendarEvent>> getEvents() {
    List<CalendarEvent> sourceList = ...
    return Observable.from(sourceList) //emits each item separately
            .concatMap(calendarEvent -> applyPlaceName(calendarEvent)) //fetches places  and applies them for each item
            //fyi: concatMap executes requests sequentially, if you want do it in parallel - use flatMap instead
            .toList(); //collects items to list
}

//somewhere in your Networking class
public Observable<CalendarEvent> applyPlaceName(CalendarEvent calendarEvent) {
    return Observable ... //do network call and apply placeName on item
}

//p.s. don't forget to apply appropriate Schedulers

09-27 23:59