RXJava2:链接改造请求的正确模式 [英] RXJava2: correct pattern to chain retrofit requests

查看:144
本文介绍了RXJava2:链接改造请求的正确模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总的来说,我对 RXJava 比较陌生(实际上只是开始将它与 RXJava2 一起使用),而且我能找到的大多数文档往往是 RXJava1;我现在通常可以在两者之间进行转换,但是整个 Reactive 的东西太大了,它是一个压倒性的 API,有很好的文档(如果你能找到的话).我正在尝试简化我的代码,我想用婴儿的步骤来做到这一点.我要解决的第一个问题是这个我在当前项目中经常使用的常见模式:

I am relatively new to RXJava in general (really only started using it with RXJava2), and most documentation I can find tends to be RXJava1; I can usually translate between both now, but the entire Reactive stuff is so big, that it's an overwhelming API with good documentation (when you can find it). I'm trying to streamline my code, an I want to do it with baby steps. The first problem I want to solve is this common pattern I do a lot in my current project:

您有一个请求,如果成功,您将用于发出第二个请求.

You have a Request that, if successful, you will use to make a second request.

如果其中一个失败,您需要能够确定哪个失败了.(主要用于显示自定义 UI 警报).

If either fails, you need to be able to identify which one failed. (mostly to display custom UI alerts).

这就是我现在通常这样做的方式:

This is how I usually do it right now:

(为简单起见省略了 .subscribeOn/observeOn)

(omitted the .subscribeOn/observeOn for simplicity)

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 中有没有更好的方法来处理这个问题?

Is there a better way to deal with this in RXJava2?

我尝试过 flatMap 和变体,甚至是 Single.zip 或类似的,但我不确定最简单和最常见的模式是什么这.

I've tried flatMap and variations and even a Single.zip or similar, but I'm not sure what the easiest and most common pattern is to deal with this.

如果您想知道 FirstRequest 会在 SecondRequest 中获取我需要的实际 Token.没有令牌就不能发出第二个请求.

In case you're wondering FirstRequest will fetch an actual Token I need in the SecondRequest. Can't make second request without the token.

推荐答案

我建议使用平面地图(和 retrolambda 如果这是一个选项).此外,如果您不对其进行任何操作,则无需保留返回值(例如 Single first).

I would suggest using flat map (And retrolambda if that is an option). Also you do not need to keep the return value (e.g Single<FirstResponse> first) if you are not doing anything with it.

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, 
          }                        
   });

这篇文章帮助我思考了风格在我一般如何构建 RxJava 中.如果可能,您希望您的链是一个高级操作列表,以便可以将其视为一系列操作/转换.

This article helped me think through styles in how I structure RxJava in general. You want your chain to be a list of high level actions if possible so it can be read as a sequence of actions/transformations.

编辑如果没有 lambda,你可以只为你的 flatMap 使用 Func1.做同样的事情只是更多的样板代码.

EDIT Without lambdas you can just use a Func1 for your flatMap. Does the same thing just a lot more boiler-plate code.

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, 
          }                        
   }); 

这篇关于RXJava2:链接改造请求的正确模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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