How to pass variable parameters to Observer?

↘锁芯ラ 提交于 2019-12-24 00:48:48

问题


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 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.

What am I doing wrong ?


回答1:


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

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



回答2:


Your method should be like:

 Observable.combineLatest(userLatObservable, userLongObervable, yourmergerfunction).flatmap( lat, long –> firstApi.get(lat, long))... Etc..

I think you problem is that how do you get the value of userLat amd userLong... Those values should first be converted to Observables to join them in the chain.



来源:https://stackoverflow.com/questions/51893435/how-to-pass-variable-parameters-to-observer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!