在rxjava中一次运行多个observable?

时间:2016-04-05 04:12:45

标签: android rx-java

我正在尝试运行3个observable并将结果设置为recyclerview适配器。是否有更好的方法可以同时运行多个observable。我有3个这样的观察者,我正在单独运行它们。有没有更好的方法来实现我想要实现的目标。

Observable<AudioItems> observableTrend = audioModel.getTrendingMusic("all-audios",userId);
    Observable<AudioItems> observableTop = audioModel.getTrendingMusic("top-audios",userId);
    Observable<AudioItems> observableRecent = audioModel.getTrendingMusic("recent-audios",userId);
    observableTrend.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
            .subscribe(new Subscriber<AudioItems>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(AudioItems audioItems) {
                    recyclerViewTrending.setAdapter(new AudiosRecyclerAdapter(getActivity(), audioItems.getAudios(), loginFlag));

                }

            });

    observableTop.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
            .subscribe(new Subscriber<AudioItems>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(AudioItems audioItems) {
                    recyclerViewTop.setAdapter(new AudiosRecyclerAdapter(getActivity(), audioItems.getAudios(), loginFlag));

                }

            });

    observableRecent.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io())
            .subscribe(new Subscriber<AudioItems>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(AudioItems audioItems) {
                    recyclerViewRecent.setAdapter(new AudiosRecyclerAdapter(getActivity(), audioItems.getAudios(), loginFlag));

                }

            });

1 个答案:

答案 0 :(得分:0)

您应该使用Zip或合并

这将抛出三个可观测量。

Zip Observables:

@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:", 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("")
                     .map(val -> "Hello");
}

Merge Observables:

@Test
public void testMerge() {
    Observable.merge(obPerson(), obPerson1())
              .subscribe(result -> showResult(result.toString()));
}

private void showResult(String s) {
    System.out.println(s);
    System.out.println(++count);
}

public Observable<Person> obPerson() {
    return Observable.just(new Person("John", 34, null));
}

public Observable<Person> obPerson1() {
    return Observable.just(new Person(null, 25, "male"));
}