How to execute 2 Observables in parallel, ignoring their results and execute next Observable

本小妞迷上赌 提交于 2020-01-24 17:33:29

问题


I have to execute 2 observable in parallel (don't care about their output), and when they both finished -> run another observable.

This is my solution, but I feel there are better ones:

rx.Observable<GameObject> obs1 = ...;
rx.Observable<GameObject> obs2 = ...;

rx.Observable.merge(obs1,obs2).takeLast(1)
.flatMap(mergeObj -> {

    return payoutStrategy.calculatePayout(gameTemplate, gameData);
}).subscribe(results -> { 
...
});

I use merge just in order to invoke the 2 obs's and then 'takeLast(1)' to ignore entering 'flatMap' twice.

This solution is far from being elegant but it works..

Any ideas how to make it better?

Thanks!


回答1:


concat is useful for doing something on completion of something else. Because the type of Observable returned by calculatePayout is presumably different you cast the empty stream to its result type:

obs1.mergeWith(obs2)
    .ignoreElements()
    .castAs(Payout.class)
    .concatWith(payoutStrategy.calculatePayout(gameTemplate, gameData))
    .subscribe( ...)

By the way if obs1 and obs2 are not async sources already then you can do this to ensure obs1 and obs2 are run in parallel:

obs1.subscribeOn(scheduler).mergeWith(obs2.subscribeOn(scheduler))
    ...

Depending on what obs2 is doing scheduler might be Schedulers.computation() or Schedulers.io().

For multiple source observables you can also do this:

Observable.just(obs1, obs2, .. , obsN)
    .flatMap(o -> o.subscribeOn(Schedulers.computation())
    .ignoreElements()
    ...


来源:https://stackoverflow.com/questions/31616874/how-to-execute-2-observables-in-parallel-ignoring-their-results-and-execute-nex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!