我有两个 Singles(getChannels 和 getEPGs),两者并行运行。在大多数情况下,getChannels 在 getEPGs 之前完成,我可以将 EPG 连接到 channel 。但是,我想处理 getEPG 在 getChannels 之前完成的情况。
换句话说,
- 两个 Singles 并行运行。
- 要连接 EPG,必须已加载 channel 。
- 如果 getEPGs 在 getChannels 之前完成,则必须等待 getChannels,然后才会调用方法
- 如果 getEPG 失败,应用流程无论如何都会继续。
如何在不依赖回调和 while 循环的情况下完成此任务?我想应该有一种 react 性的方式来处理这种情况。提前致谢。
@GET
Single<ResponseBody> getChannels(@Url String url);
@Streaming
@GET
Single<ResponseBody> getEPGs(@Url String url);
getChannels [.............................]
getEPGs [..........................................]
最佳答案
将您的函数定义到存储库中并通过 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()))
);
}
}
关于java - 并行运行两个单打并处理场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60488231/