gpt4 book ai didi

java - 在 RxJava/RxAndroid 中的可观察对象之间传递响应

转载 作者:行者123 更新时间:2023-11-29 23:46:32 25 4
gpt4 key购买 nike

我有这段代码:

getLocationObservable() // ---> async operation that fetches the location. 
// Once location is found(or failed to find) it sends it to this filter :
.filter(location -> { // ---> I want to use this location in the the onNext in the end

after finishing some calculation here, I either return 'true' and continue
to the next observable which is a Retrofit server call, or simply
return 'false' and quit.
})
.flatMap(location -> getRetrofitServerCallObservable( location )
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
new Observer<MyCustomResponse>() {
@Override
public void onSubscribe(Disposable d) {
_disposable = d;
}
@Override
public void onNext(MyCustomResponse response) {
// I want to be able to use the `location` object here
}
@Override
public void onError(Throwable e) {

}
@Override
public void onComplete() {

}
});

我希望能够在由第二个可观察对象触发的“onNext”中使用第 3 行(第一个可观察对象)中的 location 对象。我无法解决这个问题。非常感谢任何帮助。

最佳答案

代替

getRetrofitServerCallObservable( location )

您可以将结果映射为响应和位置的一对(来自您最喜欢的库):

getRetrofitServerCallObservable( location ).map(response -> Pair.create(location, response))

然后,在您的 onNext 中, 你会收到 Pair<Location,MyCustomResponse>实例。

如果您不想使用 Pair类,你可以使用 Object[] , 但如果你这样做了,请不要告诉我 :P

关于java - 在 RxJava/RxAndroid 中的可观察对象之间传递响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51300766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com