并行运行两个单打并处理场景

问题描述 投票:0回答:2

我有两个Singles(getChannel和getEPG),它们并行运行。在大多数情况下,getChannels在getEPG之前完成,我可以将EPG连接到通道。但是,我想处理在getChannels之前完成getEPG的情况。

换句话说,

  1. 两个单打都并行运行。
  2. 要连接EPG,必须已加载频道。
  3. 如果getEPGs在getChannels之前完成,则它必须等待getChannels,然后才调用一个方法
  4. 如果getEPG失败,则无论如何,应用流程都将继续。

我如何不依靠回调和while循环来完成此任务?我想应该有一种应对这种情况的反应方式。预先感谢。

@GET
Single<ResponseBody> getChannels(@Url String url);

@Streaming
@GET
Single<ResponseBody> getEPGs(@Url String url);


getChannels [.............................]

getEPGs     [..........................................]
java android reactive-programming rx-java2
2个回答
0
投票

将函数定义到存储库中,并通过viewModel访问它们。

public class Repository {
    @GET
    return Single<ResponseBody> getChannels(@Url String url);

    @Streaming
    @GET
    return Single<ResponseBody> getEPGs(@Url String url);
}

现在是ViewModel类。

public class SiteListViewModel extends BaseViewModel {

private CompositeDisposable mDisposable;
private Repository mRepository;

  public void getData() {
      mDisposable.add(
          mRepository.getChannels().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(obj -> {"CALL SECOND FUNCTION OVER HERE"

                },throwable -> Log.e("Error", e.printStackTrace()))
        );
    }
}

0
投票

2。要连接EPG,必须已加载频道。

好,

ResponseBody频道= getChannels.blockingGet();

3。如果getEPGs在getChannels之前完成,则它必须等待getChannels,然后才会调用方法

好的,等待频道并调用方法:

ResponseBody频道= getChannels.blockingGet();

aMethod(channels);

4。如果getEPG失败,则无论如何,应用程序流程都会继续。

以上陈述的确忽略了getEPGs()的结果。

如此严格地讲,您的所有条件都可以简单地表达:等待getChannels()并忽略getEPGs()。建议的两行代码完全满足条件。

© www.soinside.com 2019 - 2024. All rights reserved.