寻找Rxjava运算符将源合并到一个流中

时间:2017-11-22 07:10:23

标签: java android rx-java rx-java2

寻找Rxjava运算符将源合并到一个流中 目前有这个

  Disposable observable = Observable.concat(
                service.loadPopCells().toObservable(),
                service.loadChallangeData().toObservable(),
                service.loadUserCell().toObservable()
        )              
  .subscribe(data->sendtoViewmodel(data)); // called 3 times

我有3个流,所以订阅被称为三次,但我希望它被所有数据调用一次

希望得到类似的东西

    Disposable observable = Observable.concat(
                service.loadPopCells().toObservable(),
                service.loadChallangeData().toObservable(),
                service.loadUserCell().toObservable()
        )
        // i want to achieve something like this 
        .mapallresult(data,data2,data3){ 
         private List<SimpleCell> shots = new ArrayList<>();
         shots.add(data);
         shots.add(data2);
         shots.add(data2);
         return shots;  }
         ///
         .subscribe(dataList->sendtoViewmodel(dataList); // called once 

2 个答案:

答案 0 :(得分:2)

zip运营商将帮助您:

Observable.zip(
        service.loadPopCells().toObservable(),
        service.loadChallangeData().toObservable(),
        service.loadUserCell().toObservable(),
        (data1, data2, data3) -> Arrays.asList(data1, data2, data3))
        .subscribe(dataList -> sendtoViewmodel(dataList));
    }

甚至更短:

Observable.zip(
    service.loadPopCells().toObservable(),
    service.loadChallangeData().toObservable(),
    service.loadUserCell().toObservable(),
    Arrays::asList)
    .subscribe(this::sendtoViewmodel);

答案 1 :(得分:1)

我认为您需要的是使用zip运算符,以同步或异步方式运行所有可观察的操作,然后一起得到结果

查看此示例

  /**
     * In this example the the three observables will be emitted sequentially and the three items will be passed to the pipeline
     */
    @Test
    public void testZip() {
        long start = System.currentTimeMillis();
        Observable.zip(obString(), obString1(), obString2(), (s, s2, s3) -> s.concat(s2)
                .concat(s3))
                .subscribe(result -> showResult("Sync in:", start, result));
    }


    public void showResult(String transactionType, long start, String result) {
        System.out.println(result + " " +
                transactionType + String.valueOf(System.currentTimeMillis() - start));
    }

    public Observable<String> obString() {
        return Observable.just("")
                .doOnNext(val -> {
                    System.out.println("Thread " + Thread.currentThread()
                            .getName());
                })
                .map(val -> "Hello");
    }

    public Observable<String> obString1() {
        return Observable.just("")
                .doOnNext(val -> {
                    System.out.println("Thread " + Thread.currentThread()
                            .getName());
                })
                .map(val -> " World");
    }

    public Observable<String> obString2() {
        return Observable.just("")
                .doOnNext(val -> {
                    System.out.println("Thread " + Thread.currentThread()
                            .getName());
                })
                .map(val -> "!");
    }

或同样的例子async

private Scheduler scheduler;
private Scheduler scheduler1;
private Scheduler scheduler2;

/**
 * Since every observable into the zip is created to subscribeOn a different thread, it´s means all of them will run in parallel.
 * By default Rx is not async, only if you explicitly use subscribeOn.
 */
@Test
public void testAsyncZip() {
    scheduler = Schedulers.newThread();
    scheduler1 = Schedulers.newThread();
    scheduler2 = Schedulers.newThread();
    long start = System.currentTimeMillis();
    Observable.zip(obAsyncString(), obAsyncString1(), obAsyncString2(), (s, s2, s3) -> s.concat(s2)
            .concat(s3))
            .subscribe(result -> showResult("Async in:", start, result));
}

private Observable<String> obAsyncString() {
    return Observable.just("")
            .observeOn(scheduler)
            .doOnNext(val -> {
                System.out.println("Thread " + Thread.currentThread()
                        .getName());
            })
            .map(val -> "Hello");
}

private Observable<String> obAsyncString1() {
    return Observable.just("")
            .observeOn(scheduler1)
            .doOnNext(val -> {
                System.out.println("Thread " + Thread.currentThread()
                        .getName());
            })
            .map(val -> " World");
}

private Observable<String> obAsyncString2() {
    return Observable.just("")
            .observeOn(scheduler2)
            .doOnNext(val -> {
                System.out.println("Thread " + Thread.currentThread()
                        .getName());
            })
            .map(val -> "!");
}