如何将可变参数传递给观察者? [英] How to pass variable parameters to Observer?

查看:73
本文介绍了如何将可变参数传递给观察者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个与flatMap合并的Observer.第一个观察者返回一个值,该值在第二个观察者被调用时使用.

I have two Observers that are merged with a flatMap. The first observer returns a value that is used when the second is called.

Observable<Integer> mergedObservers = firstAPI.getFirstInfo(userLat, userLong)
.flatMap(resultFirstObservable -> {
     try {
        return secondApi.getSecondInfo(resultFirstObservable.body().string(), "3") 
        .onErrorResumeNext(e -> {
            e.printStackTrace();
             return secondApi.getSecondInfo("defaultValue", "3");
          });
      } catch (IOException e) {
           e.printStackTrace();
           secondApi.getSecondInfo("defaultValue", "3") 
        .onErrorResumeNext(e -> {
            e.printStackTrace();
             return secondApi.getSecondInfo("defaultValue", "3");
          });
    });
}
}, (resultFirstObservable, resultSecondObservable) -> {
try {
    return transformToWhatINeed(resultSecondObservable.body().string());
} catch (IOException ex) {
    ex.printStackTrace();
    return transformToWhatINeed([]);
}
});

userLat和userLong在我的方法之外声明,并且在活动打开期间被更改,但是我的订阅只考虑了它们的第一个值.我本来希望每次有一个新呼叫时,它们都会采用最新的值.

userLat and userLong are declared outside my method and are changed during the time the activity is open, but my Subscription takes into account only the first value of these. I would have expected that each time there's a new call, they will take the newest values.

我在做什么错了?

推荐答案

如果我了解您使用Observable.defer正确地解决了问题

If I understand you problem correctly using Observable.defer should solve problem

Observable<Integer> mergedObservers = Observable.defer {
    firstAPI.getFirstInfo(userLat, userLong)
}.flatMap ...

这篇关于如何将可变参数传递给观察者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆