如何使用RxJava链接多个observable?

时间:2017-06-17 18:05:16

标签: java android rx-java rx-android

我正在尝试在调用活动onDestroy时执行一些操作。我想启动一个带有ID的Observable,然后从Realm检索一些数据,并根据检索到的数据对后端执行HTTP请求,然后将检索到的数据存储到以起始id给出的行。

要点:

  1. 从id
  2. 的数据库中检索数据
  3. 使用数据执行后端请求
  4. 将检索到的数据存储到具有步骤1中的ID的行
  5. 图形:

    expected flow

    代码: 我最终遇到并陷入困境

    Observable.just(id)
            .observeOn(Schedulers.io())
            .map(new Function<String, Person>() {
                @Override
                public Person apply(@NonNull String id) throws Exception {
                    Realm realm = Realm.getDefaultInstance();
    
                    Person person = realm.copyFromRealm(realm.where(Person.class).equalTo("id", id).findFirst());
    
                    realm.close();
    
                    return person;
                }
            })
            .switchMap(new Function<Person, Observable<Directions>>() {
                @Override
                public Observable<Directions> apply(@NonNull Person person) throws Exception {
                    return Utils.getRemoteService().getDirections(person.getAddress()); // retrofit
                }
            })
            .map(new Function<Directions, Object>() {
                @Override
                public Object apply(@NonNull Directions directions) throws Exception {
    
                    // how do I get the id here to store the data to the correct person
    
                    return null;
                }
            })
            .subscribe();
    

    注意:

    • POJO是虚构的
    • 这是我第一次使用RxJava

1 个答案:

答案 0 :(得分:0)

信息必须在流中传递,它可以像下面这样完成。当你将它包装在一个类而不是Pair时,它会更具可读性。

 Observable.just(id)
            .observeOn(Schedulers.io())
            .map(new Function<String, Person>() {
                @Override
                public Person apply(@NonNull String id) throws Exception {
                    Realm realm = Realm.getDefaultInstance();

                    Person person = realm.copyFromRealm(realm.where(Person.class).equalTo("id", id).findFirst());

                    realm.close();

                    return person;
                }
            })
            .switchMap(new Function<Person, Observable<Directions>>() {
                @Override
                public Observable<Directions> apply(@NonNull Pair<String, Person> pair) throws Exception {
                    // assuming that id is available by getId
                    return Pair(person.getId(), Utils.getRemoteService().getDirections(person.getAddress())); // retrofit
                }
            })
            .map(new Function<Pair<String, Directions>, Object>() {
                @Override
                public Object apply(@NonNull Pair<String, Directions> pair) throws Exception {

                    // how do I get the id here to store the data to the correct person
                    // pair.first contains the id
                    // pair.second contains the Directions
                    return null;
                }
            })
            .subscribe();
相关问题