按顺序执行Observable

时间:2015-10-19 16:35:49

标签: rx-java rx-android

我正在尝试在Android应用程序中实现rxJava,我正试图绕过这个。这是我想要满足的场景:

  1. 调用网络服务以获取计划详情
  2. 使用第一个网络服务调用下一个网络服务来确定其实时流媒体状态(无,转码,完成等)
  3. 将响应组合并转换为一个对象,该对象将被发送回ui以供显示。
  4. 理想情况下,如果步骤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));
    }
    

1 个答案:

答案 0 :(得分:0)

  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()
相关问题