【问题标题】:How to chain multiple observables with RxJava?如何使用 RxJava 链接多个可观察对象?
【发布时间】:2017-06-17 18:05:15
【问题描述】:

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

总结:

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

图形:

代码: 我最终遇到了什么并被卡住了

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

【问题讨论】:

    标签: java android rx-java rx-android


    【解决方案1】:

    信息必须在流中传递,可以像下面这样完成。当您将其包装在一个类中而不是 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();
    

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      相关资源
      最近更新 更多