RxJava Android 序列打印数据

RxJava Android sequence print data

我正在 Android 学习 RxJava。

我有:

Observable<Country> getCountries(){} // returns Countries
Observable<City> getCapital(int countryId){} // returns capital by country id

我要:

打印 getCountries() 返回的所有国家名称。然后从 getCapital() 方法打印每个国家的首都名称,id=1 的国家除外。这一切都应该在一个链中。

例如:

private Observable<Country> getCountries(){
    return Observable.just(
            new Country(0, "Luxemburg"),
            new Country(1, "Netherlands"),
            new Country(2, "Norway"),
            new Country(3, "India"),
            new Country(4, "Italy")
    );
}
private Observable<City> getCapital(int countryId){
    City city;
    switch (countryId){
        case 0: city = new City("Luxembrug");
            break;
        case 1: city = new City("Amsterdam");
            break;
        case 2: city = new City("Oslo");
            break;
        case 3: city = new City("Delhi");
            break;
        case 4: city = new City("Rome");
            break;
        default:
            city = new City("");
            break;
    }
    return Observable.just(city);
}


getCountries()
      .doOnNext(country -> Log.d("TAG", "Country: id="+country.getId()+" name="+country.getName()))
      .filter(country -> country.getId()!=1)
      .flatMap(country -> getCapital(country.getId()))
      .subscribe(city -> Log.d("TAG", "City: "+city.getName()));

我想得到的:

D: Country: id=0 name=Luxemburg
D: Country: id=1 name=Netherlands
D: Country: id=2 name=Norway
D: Country: id=3 name=India
D: Country: id=4 name=Italy
D: City: Amsterdam
D: City: Oslo
D: City: Delhi
D: City: Rome

我得到的:

D: Country: id=0 name=Luxemburg
D: Country: id=1 name=Netherlands
D: City: Amsterdam
D: Country: id=2 name=Norway
D: City: Oslo
D: Country: id=3 name=India
D: City: Delhi
D: Country: id=4 name=Italy
D: City: Rome

我怎样才能做到这一点?

我应该首先获取所有国家,打印出来,然后获取这些国家的首都。但是我不明白我怎么能在一条链上做到这一点..

感谢任何提示!

假设您无法更改 getCountries()getCapital(int countryId) 方法,您可以这样实现:

RxJava 1x:

    getCountries()
            .toList()
            .doOnNext(countries -> {
                for (Country country : countries) {
                    Log.d("TAG", "Country: id=" + country.getId() + " name=" + country.getName());
                }
            })
            .flatMap(Observable::from)
            .filter(country -> country.getId() != 1)
            .flatMap(new Func1<Country, Observable<City>>() {
                @Override public Observable<City> call(Country country) {
                    return getCapital(country.getId());
                }
            })
            .subscribe(city -> Log.d("TAG", "City: " + city.getName()));

RxJava 2x:

    getCountries()
            .toList()
            .doOnSuccess(countries -> {
                for (Country country : countries) {
                    Log.d("TAG", "Country: id=" + country.getId() + " name=" + country.getName());
                }
            })
            .toObservable()
            .flatMap(Observable::fromIterable)
            .filter(country -> country.getId() != 1)
            .flatMap(new Function<Country, ObservableSource<City>>() {
                @Override
                public ObservableSource<City> apply(@NonNull Country country) throws Exception {
                    return getCapital(country.getId());
                }
            })
            .subscribe(city -> Log.d("TAG", "City: " + city.getName()));