【问题标题】:Executing Observables in sequence按顺序执行 Observables
【发布时间】:2016-05-31 23:08:03
【问题描述】:

我正在尝试在 android 应用程序中实现 rxJava,我正在尝试解决这个问题。这是我想要满足的场景:

  1. 调用 Web 服务以获取程序详细信息
  2. 使用第一个 Web 服务中的数据调用下一个 Web 服务以确定其实时流状态(无、转码、完成等)
  3. 将响应组合并转换为一个对象,该对象被发送回 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));
}

【问题讨论】:

    标签: rx-java rx-android


    【解决方案1】:
    1. .firstObservable.flatmap( ... ) //will have access to first result
    2. .filter( ... ) //will filter out the ones that dont fit your condition)
    3. .map( ... ) //will have access to second result)
    4. .subscribe()

    【讨论】:

    • 不确定格式被编辑的原因。这是一个单一的代码链,而不是一个编号严重的步骤。
    • 感谢@MikeN,看起来这可能有效。一个问题,secondObservable 发出不同的类型,我真正需要的是将它添加到 firstObservable 的属性中。这可能吗?
    • flatmap 函数获取第一个 observable 的结果作为输入,您可以将该结果传递给第二个 observable getter,然后传播到该 observable 的结果。
    • 我用我目前的位置更新了原始票。
    猜你喜欢
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多