适用于Android的rxJava2中的可观察的zip

时间:2018-11-21 12:59:03

标签: android rx-java observable rx-java2

我曾经在Android的rxJava1中开发了很多东西,而且我喜欢zip Operator。不幸的是我无法在rxJava2上运行它。有人可以帮我吗?我正在尝试执行以下

Observable
    .zip(
        Observable.just(“1”), 
        Observable.just(“2"), 
        Observable.just(“3”), 
        Observable.just(“4"),
        new Func4<String, String, String, String, MyResult>() {
           @Override
           public MyResult call(String string1, String string2, String string3, String string4) {
               return new MyResult(string1, string2, string3, string4);
           }
       });

3 个答案:

答案 0 :(得分:1)

将其更改为

Java实现:

Flowable.zip(
                Flowable.just("1"),
                Flowable.just("2"),
                Flowable.just("3"),
                Flowable.just("4"),
                new Function4<String, String, String, String, MyResult >() {
                    @Override 
                    public MyResult apply(t1: String, t2: String, t3: String, t4: String) {
                        // return MyResult
                    }
                })

Kotlin的实现:

Flowable.zip(
                Flowable.just("1"),
                Flowable.just("2"),
                Flowable.just("3"),
                Flowable.just("4"),
                object : Function4<String, String, String, String, MyResult > {
                    override fun apply(t1: String, t2: String, t3: String, t4: String): MyResult {
                        // return MyResult
                    }
                })

答案 1 :(得分:0)

    Observable.zip(Observable.just("1"),
            Observable.just("2"),
            Observable.just("3"),
            Observable.just("4"),
            new Function4<String, String, String, String, MyResult>() {

                @Override
                public MyResult apply(String s, String s2, String s3, String s4) throws Exception {
                    return new MyResult(s, s2, s3, s4);
                }
            });

并确保您使用

中的Observable
import io.reactivex.Observable;

答案 2 :(得分:0)

您可以尝试一下,希望它能起作用。

Observable.zip(getResponse1(), getResponse2(),
    object : BiFunction<Res1, Resp2, Pair<Res1, Resp2>> {
      override fun apply(t1: Res1, t2: Res1): Pair<Res1, Res2> {
        return Pair(t1, t2)
      }
    })