RXJava2:链接改造请求的正确模式
RXJava2: correct pattern to chain retrofit requests
总的来说,我对 RXJava 比较陌生(真的只是开始使用它与 RXJava2),而且我能找到的大多数文档往往是 RXJava1;我现在通常可以在两者之间进行转换,但是整个 Reactive 的东西是如此之大,以至于它是一个压倒性的 API 和好的文档(当你能找到它的时候)。我正在尝试简化我的代码,我想用一些小步骤来完成。我要解决的第一个问题是我在当前项目中经常使用的这种常见模式:
您有一个请求,如果成功,您将用来发出第二个请求。
如果其中一个失败,您需要能够确定是哪个失败了。 (主要是为了显示自定义 UI 警报)。
这就是我现在通常做的事情:
(为简单起见省略了 .subscribeOn/observeOn
)
Single<FirstResponse> first = retrofitService.getSomething();
first
.subscribeWith(
new DisposableSingleObserver<FirstResponse>() {
@Override
public void onSuccess(final FirstResponse firstResponse) {
// If FirstResponse is OK…
Single<SecondResponse> second =
retrofitService
.getSecondResponse(firstResponse.id) //value from 1st
.subscribeWith(
new DisposableSingleObserver<SecondResponse>() {
@Override
public void onSuccess(final SecondResponse secondResponse) {
// we're done with both!
}
@Override
public void onError(final Throwable error) {
//2nd request Failed,
}
});
}
@Override
public void onError(final Throwable error) {
//firstRequest Failed,
}
});
在 RXJava2 中有没有更好的方法来处理这个问题?
我已经尝试了 flatMap
和变体,甚至 Single.zip
或类似的方法,但我不确定处理此问题的最简单和最常见的模式是什么。
如果您想知道 FirstRequest 将获取我在 SecondRequest 中需要的实际 Token
。没有令牌无法发出第二个请求。
我建议使用平面地图(如果可以的话 retrolambda)。
此外,如果您不对其进行任何操作,则无需保留 return 值(例如 Single<FirstResponse> first
)。
retrofitService.getSomething()
.flatMap(firstResponse -> retrofitService.getSecondResponse(firstResponse.id)
.subscribeWith(new DisposableSingleObserver<SecondResponse>() {
@Override
public void onSuccess(final SecondResponse secondResponse) {
// we're done with both!
}
@Override
public void onError(final Throwable error) {
// a request request Failed,
}
});
This 这篇文章帮助我从总体上思考了如何构建 RxJava 的样式。如果可能,您希望您的链成为高级操作的列表,以便可以将其读取为 actions/transformations.
的序列
编辑
如果没有 lambda,你可以只使用 Func1
作为你的 flatMap。做同样的事情只是更多的样板代码。
retrofitService.getSomething()
.flatMap(new Func1<FirstResponse, Observable<SecondResponse> {
public void Observable<SecondResponse> call(FirstResponse firstResponse) {
return retrofitService.getSecondResponse(firstResponse.id)
}
})
.subscribeWith(new DisposableSingleObserver<SecondResponse>() {
@Override
public void onSuccess(final SecondResponse secondResponse) {
// we're done with both!
}
@Override
public void onError(final Throwable error) {
// a request request Failed,
}
});
这对你不起作用吗?
retrofitService
.getSomething()
.flatMap(firstResponse -> retrofitService.getSecondResponse(firstResponse.id))
.doOnNext(secondResponse -> {/* both requests succeeded */})
/* do more stuff with the response, or just subscribe */
总的来说,我对 RXJava 比较陌生(真的只是开始使用它与 RXJava2),而且我能找到的大多数文档往往是 RXJava1;我现在通常可以在两者之间进行转换,但是整个 Reactive 的东西是如此之大,以至于它是一个压倒性的 API 和好的文档(当你能找到它的时候)。我正在尝试简化我的代码,我想用一些小步骤来完成。我要解决的第一个问题是我在当前项目中经常使用的这种常见模式:
您有一个请求,如果成功,您将用来发出第二个请求。
如果其中一个失败,您需要能够确定是哪个失败了。 (主要是为了显示自定义 UI 警报)。
这就是我现在通常做的事情:
(为简单起见省略了 .subscribeOn/observeOn
)
Single<FirstResponse> first = retrofitService.getSomething();
first
.subscribeWith(
new DisposableSingleObserver<FirstResponse>() {
@Override
public void onSuccess(final FirstResponse firstResponse) {
// If FirstResponse is OK…
Single<SecondResponse> second =
retrofitService
.getSecondResponse(firstResponse.id) //value from 1st
.subscribeWith(
new DisposableSingleObserver<SecondResponse>() {
@Override
public void onSuccess(final SecondResponse secondResponse) {
// we're done with both!
}
@Override
public void onError(final Throwable error) {
//2nd request Failed,
}
});
}
@Override
public void onError(final Throwable error) {
//firstRequest Failed,
}
});
在 RXJava2 中有没有更好的方法来处理这个问题?
我已经尝试了 flatMap
和变体,甚至 Single.zip
或类似的方法,但我不确定处理此问题的最简单和最常见的模式是什么。
如果您想知道 FirstRequest 将获取我在 SecondRequest 中需要的实际 Token
。没有令牌无法发出第二个请求。
我建议使用平面地图(如果可以的话 retrolambda)。
此外,如果您不对其进行任何操作,则无需保留 return 值(例如 Single<FirstResponse> first
)。
retrofitService.getSomething()
.flatMap(firstResponse -> retrofitService.getSecondResponse(firstResponse.id)
.subscribeWith(new DisposableSingleObserver<SecondResponse>() {
@Override
public void onSuccess(final SecondResponse secondResponse) {
// we're done with both!
}
@Override
public void onError(final Throwable error) {
// a request request Failed,
}
});
This 这篇文章帮助我从总体上思考了如何构建 RxJava 的样式。如果可能,您希望您的链成为高级操作的列表,以便可以将其读取为 actions/transformations.
的序列编辑
如果没有 lambda,你可以只使用 Func1
作为你的 flatMap。做同样的事情只是更多的样板代码。
retrofitService.getSomething()
.flatMap(new Func1<FirstResponse, Observable<SecondResponse> {
public void Observable<SecondResponse> call(FirstResponse firstResponse) {
return retrofitService.getSecondResponse(firstResponse.id)
}
})
.subscribeWith(new DisposableSingleObserver<SecondResponse>() {
@Override
public void onSuccess(final SecondResponse secondResponse) {
// we're done with both!
}
@Override
public void onError(final Throwable error) {
// a request request Failed,
}
});
这对你不起作用吗?
retrofitService
.getSomething()
.flatMap(firstResponse -> retrofitService.getSecondResponse(firstResponse.id))
.doOnNext(secondResponse -> {/* both requests succeeded */})
/* do more stuff with the response, or just subscribe */