简体   繁体   English

RxJava2通过FlatMap在Single上传递数据

[英]RxJava2 passing data through flatMap on a Single

In rxjava 1 there Observable had this flatmap method 在rxjava 1中,Observable具有此flatmap方法

public final Observable flatMap(Func1 collectionSelector, Func2 resultSelector) 公共最终的Observable flatMap(Func1 collectionSelector,Func2 resultSelector)

That allowed you to pass/combine the initial result to the flatmap subscriber. 这使您可以将初始结果传递/组合到平面图订户。

How can I achieve the same result with RxJava2? 如何使用RxJava2获得相同的结果?

I have a Single that emits A , I need to get B based on A and then use both A and B to perform an action. 我有一个发出A的Single,我需要根据A获取B ,然后同时使用AB来执行操作。

You have the same method on RxJava2, both on Observable and Flowable , 你有RxJava2同样的方法,无论是在可观测可流动
but, in both RxJava1 and 2, there is no such operator for Single , you can transform Single to Observable and then apply this operators. 但是,在RxJava1和2中,都没有用于Single此类运算符,您可以将Single转换Observable ,然后应用此运算符。

Have you tried CombineLatest ( http://reactivex.io/documentation/operators/combinelatest.html ) 您是否尝试过CombineLatest( http://reactivex.io/documentation/operators/combinelatest.html

Basically you can emit A and B and then return another object based on the function result: 基本上,您可以发出A和B,然后根据函数结果返回另一个对象:

RXJava1 RXJava1

Observable
  .combineLatest([Add here your A observable],
                 [Add here your B observable],
                 new Func2<A, B, Result>() {
                    @Override
                    public Result call(A a, B b) {
                        //Do your stuff here combining both results and return the result expected
                    }
                 })

RXJava2 RXJava2

Observable
  .combineLatest([Add here your A observable],
                 [Add here your B observable],
                 new BiFunction<A, B, Result>() {
                    @Override
                    public Result apply(A a, B b) throws Exception {
                        //Do your stuff here combining both results and return the result expected
                    }
                 })

The answer by Yosriz is correct but to add a code example: Yosriz的答案是正确的,但要添加一个代码示例:

Assuming the following: 假设以下内容:

class A {}

class B {}

class AB {
    private final A a;
    private final B b;

    AB(A a, B b) {
        this.a = a;
        this.b = b;
    }
}

interface AbRepository {

    Single<A> getA();

    Single<B> getB(A a);
}

Note that the method getB requires an A as a parameter. 请注意,方法getB需要A作为参数。

Then you can do: 然后,您可以执行以下操作:

abRepository.getA()
        .toObservable()
        .flatMap(new Function<A, ObservableSource<B>>() {
            @Override
            public ObservableSource<B> apply(A a) throws Exception {
                return abRepository.getB(a)
                        .toObservable();
            }
        }, new BiFunction<A, B, AB>() {
            @Override
            public AB apply(A a, B b) throws Exception {
                return new AB(a, b);
            }
        })
        .firstOrError();

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

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