按顺序执行 Observable
Executing Observables in sequence
我正在尝试在 android 应用程序中实现 rxJava,我正在努力解决这个问题。这是我要满足的场景:
- 调用网络服务获取程序详细信息
- 使用第一个 Web 服务的数据调用下一个 Web 服务以确定其直播状态(none、转码、完成等)
- 将响应合并并转换为一个对象,该对象被发送回 ui 以供显示。
理想情况下,如果第 2 步中的调用包含响应,但未完成,则可以持续 运行 直到该作业在后端完成。但我正在采取一些小步骤,接下来会解决这个问题。
更新:2015-10-21
到目前为止,这就是我所在的位置。我大概可以洗干净了
public Observable<Program> recordedProgram( int chanId, DateTime startTime ) {
final DvrDataStore dvrDataStore = this.dvrDataStoreFactory.create( chanId, startTime );
final ContentDataStore contentDataStore = this.contentDataStoreFactory.createMasterBackendDataStore();
Observable<ProgramEntity> programEntity = dvrDataStore.recordedProgramEntityDetails( chanId, startTime );
Observable<List<LiveStreamInfoEntity>> liveStreamInfoEntity = programEntity
.flatMap(recordedProgramEntity -> contentDataStore.liveStreamInfoEntityList(recordedProgramEntity.getFileName()));
Observable<ProgramEntity> recordedProgramEntity = Observable.zip(programEntity, liveStreamInfoEntity, new Func2<ProgramEntity, List<LiveStreamInfoEntity>, ProgramEntity>() {
@Override
public ProgramEntity call(ProgramEntity programEntity, List<LiveStreamInfoEntity> liveStreamInfoEntityList) {
if (null != liveStreamInfoEntityList && !liveStreamInfoEntityList.isEmpty()) {
programEntity.setLiveStreamInfoEntity(liveStreamInfoEntityList.get( 0 ) );
}
return programEntity;
}
});
return recordedProgramEntity.map(recordedProgram ->
this.programEntityDataMapper.transform(recordedProgram));
}
.firstObservable.flatmap( ... ) //will have access to first result
.filter( ... ) //will filter out the ones that dont fit your condition)
.map( ... ) //will have access to second result)
.subscribe()
我正在尝试在 android 应用程序中实现 rxJava,我正在努力解决这个问题。这是我要满足的场景:
- 调用网络服务获取程序详细信息
- 使用第一个 Web 服务的数据调用下一个 Web 服务以确定其直播状态(none、转码、完成等)
- 将响应合并并转换为一个对象,该对象被发送回 ui 以供显示。
理想情况下,如果第 2 步中的调用包含响应,但未完成,则可以持续 运行 直到该作业在后端完成。但我正在采取一些小步骤,接下来会解决这个问题。
更新:2015-10-21 到目前为止,这就是我所在的位置。我大概可以洗干净了
public Observable<Program> recordedProgram( int chanId, DateTime startTime ) {
final DvrDataStore dvrDataStore = this.dvrDataStoreFactory.create( chanId, startTime );
final ContentDataStore contentDataStore = this.contentDataStoreFactory.createMasterBackendDataStore();
Observable<ProgramEntity> programEntity = dvrDataStore.recordedProgramEntityDetails( chanId, startTime );
Observable<List<LiveStreamInfoEntity>> liveStreamInfoEntity = programEntity
.flatMap(recordedProgramEntity -> contentDataStore.liveStreamInfoEntityList(recordedProgramEntity.getFileName()));
Observable<ProgramEntity> recordedProgramEntity = Observable.zip(programEntity, liveStreamInfoEntity, new Func2<ProgramEntity, List<LiveStreamInfoEntity>, ProgramEntity>() {
@Override
public ProgramEntity call(ProgramEntity programEntity, List<LiveStreamInfoEntity> liveStreamInfoEntityList) {
if (null != liveStreamInfoEntityList && !liveStreamInfoEntityList.isEmpty()) {
programEntity.setLiveStreamInfoEntity(liveStreamInfoEntityList.get( 0 ) );
}
return programEntity;
}
});
return recordedProgramEntity.map(recordedProgram ->
this.programEntityDataMapper.transform(recordedProgram));
}
.firstObservable.flatmap( ... ) //will have access to first result
.filter( ... ) //will filter out the ones that dont fit your condition)
.map( ... ) //will have access to second result)
.subscribe()