如何在RxJava Android中将每个项目添加到列表中
我的代码如下如何在RxJava Android中将每个项目添加到列表中,java,android,lambda,retrofit,rx-java,Java,Android,Lambda,Retrofit,Rx Java,我的代码如下 Observable<List<Appointment>> callAppointments = appointmentServiceRx.appointmentService.getConfirmedAppointments(user_id); callAppointments .flatMapIterable(appointments -> appointments) .flatMap(app
Observable<List<Appointment>> callAppointments = appointmentServiceRx.appointmentService.getConfirmedAppointments(user_id);
callAppointments
.flatMapIterable(appointments -> appointments)
.flatMap(app -> Observable.zip(
app,
patientServiceRx.patientService.getPatientById(app.patient_id),
servicesRestRx.servicesAPIServiceRx.getSubserviceById(app.subservice_id),
(appointment, patient, subservice) -> this.createListItem(appointment, patient, subservice)
))
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
可观察时间线:活动空闲
是一种转移视线的行为,与您的问题无关
您似乎在约会中使用了血腥的字段!您只有一个AppointmentListItem实例,这就是为什么
编辑:Observable::zip
是你的朋友!我是这样做的:
callAppointments
.flatMapIterable(appointments -> appointments)
.flatMap(app -> Observable.zip(
app,
patientServiceRx.getService().getPatientById(app.patient_id),
servicesRestRx.getService().getSubserviceById(app.subservice_id),
(app, patient, subservice) -> this.createListItem(app, patient, subservice))
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(list -> {/*set adapter*/}, error -> {/* handle error */});
这样做的另一个好处是,每次约会同时进行两次通话。请记住,
flatMap
不一定保留原始顺序,因此您可能会以与约会不同的顺序使用AppointmentListItem。啊,我明白了。那么,如何将每个结果调用检索到约会对象中,而不是使用字段?有什么建议吗?我编辑了原始帖子。我还想知道为什么要使用getService()
调用;为什么不直接注入服务?请您解释一下关于直接注入服务的更多内容?在这方面我还是个新手。与其做整个servicesRestRx.getService().getSubserviceById(app.subservice\u id)
dance,不如研究一下你的DI系统如何直接使用servicesClient.getSubserviceById(app.subservice\u id)
并跳过显式的.setService()
stage-DI应该在这方面帮助你,而不是成为你需要跨越的额外障碍。这个howewer是独立的。啊,明白了。顺便说一句,我试着使用你的解决方案,但也许我弄错了。我更新了我的代码供您参考,请告诉我如何正确操作。
callAppointments
.flatMapIterable(appointments -> appointments)
.flatMap(app -> Observable.zip(
app,
patientServiceRx.getService().getPatientById(app.patient_id),
servicesRestRx.getService().getSubserviceById(app.subservice_id),
(app, patient, subservice) -> this.createListItem(app, patient, subservice))
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(list -> {/*set adapter*/}, error -> {/* handle error */});