gpt4 book ai didi

java - RxJava缝制两个查询

转载 作者:行者123 更新时间:2023-12-01 09:13:35 25 4
gpt4 key购买 nike

如何在 RxJava 中缝合两个 Observable ?

Observable<List<CalendarEvent>> ,对于每个 CalendarEvent,我想做网络操作来读取(lat,lon)并获取地名,然后将地名组合回 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

关于java - RxJava缝制两个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40748827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com