说我有那些过于类如何加载relationed对象与RxJava
class Event {
int id;
String name;
List<Integer> facilityIds;
List<Facility> facilities; // Empty, the one I need to load
}
class Facility {
int id;
String name;
}
目标:打印出他们的设施的名称每个事件。
约束:RxJava2,设施只能由一个(getFacility(facilityId)
)
加载一个从Observable<Event>
,我无法找到我的路周围装载设施,并设置他们回到各自的活动。
基本上我在想是这样的:
Observable<Event> events;
events
.map(Event::getFacilityIds)
.flatMap(Observable::fromIterable)
.map(facilityId -> service.getFacility(facilityId))
. // somehow get the event reference and
// so some event.addFacility() or something similar
然后我去相亲,不能找到一种方法将它们链接回事件。我也想过使用zip
,它可能是一个解决方案,但我没有找到一种方法来保存和事件引用,以便稍后将设施设置给他们。
什么是被动的方式去?任何提示将不胜感激。
使用* flatMap *而不是* map *会更好吗?因为我认为'service.getFacility'可能需要一些时间来回应... –
@ oliv37是的,你是对的。我将提供平行版本。你知道如何在平行执行的情况下保证启动顺序吗?因为我从来没有尝试过。 – Beloo
我得到了一个稍微不同的解决方案,但是对于'forEach'的使用让我走上了正确的道路,谢谢。 – oldergod