简体   繁体   English

RxJava:当两个不同结果类型的observable完成时调用函数

[英]RxJava: call function when two observables with different result types are completed

I'm working on an Android application that retrieves two different objects via an API that I access via RxJava Observables.我正在开发一个 Android 应用程序,该应用程序通过我通过 RxJava Observables 访问的 API 检索两个不同的对象。 To update the UI, I need both results.要更新 UI,我需要两个结果。

How can I run a function as soon as both Observables have completed?两个 Observable 都完成后,如何运行函数? It seems like functions like merge are doing what I'm planning, but as far as I can see they only work for Observables with the same result type or need a composite object that can represent both types.看起来像merge这样的函数正在做我计划的事情,但据我所知,它们只适用于具有相同结果类型的 Observables 或者需要一个可以表示两种类型的复合对象。

A simple example:一个简单的例子:

Observable.just("Hello world")
        .subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println(s);
            }
        });
Observable.just(1, 2, 3, 4, 5, 6)
        .subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer i) {
                System.out.println(i);
            }
        });

What would I need to do to run System.out.println("Finished!") as soon as both Observables have completed their task?一旦两个 Observable 都完成了他们的任务,我需要做什么来运行System.out.println("Finished!")

In the particular case of my Android application, I could simply store the results in the actual class, have an updateUi function that only does work when all required data already arrived and call this function from both onCompleted calls, but I feel there is a better way.在我的 Android 应用程序的特殊情况下,我可以简单地将结果存储在实际类中,有一个 updateUi 函数,该函数仅在所有必需的数据都已到达时才起作用,并从两个 onCompleted 调用中调用此函数,但我觉得有一个更好的道路。

Version 2: emitted items are processed as "side" actions, only onCompleted event goes through to the merged observable.版本 2:发出的项目作为“侧”动作处理,只有onCompleted事件通过合并的 observable。

Observable<String> stringObservable = Observable.just("Hello world")
        .doOnNext(System.out::println)
        .ignoreElements();
Observable<Integer> integerObservable = Observable.just(1, 2, 3, 4, 5, 6)
        .doOnNext(System.out::println)
        .ignoreElements();

Observable.merge(stringObservable, integerObservable)
        .subscribe(new Subscriber<Object>() {
            @Override
            public void onCompleted() {
                System.out.println("Finished!");
            }

            @Override
            public void onError(Throwable throwable) {
                /*optionally handle if one of the observables calls onError()*/
            }

            @Override
            public void onNext(Object o) { /*not called*/ }
        });

I think, you need a TaskCoordinator.我认为,您需要一个 TaskCoordinator。 Keep a simple counter in the taskCoordinator.在 taskCoordinator 中保留一个简单的计数器。 Every time you signal a success, counter will be decremented.每次您发出成功信号时,计数器都会递减。 When the counter reaches zero, the callback will be executed.当计数器达到零时,将执行回调。 You can see my implementation like this here https://github.com/codefacts/crm-common/blob/master/src/main/java/io/crm/util/TaskCoordinator.java你可以在这里看到我的实现https://github.com/codefacts/crm-common/blob/master/src/main/java/io/crm/util/TaskCoordinator.java

It is better because, Every time you coordinate tasks, It will come to use.更好,因为,每次协调任务时,它都会用到。 Reactive Stream does not have built in support for task coordination. Reactive Stream 没有内置支持任务协调。 So a promise library or Task Coordination implementation will be helpful in this case.因此,在这种情况下,promise 库或任务协调实现将很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM