2017-04-11 30 views
4

我是相對較新的RXJava一般(真的只開始使用它與RXJava2),我能找到的大多數文件往往是RXJava1;我通常可以在兩者之間進行翻譯,但是整個反應性的東西非常大,它是一個壓倒性的API,有很好的文檔(當你找到它的時候)。我試圖簡化我的代碼,我想用嬰兒步驟來完成它。我想要解決的第一個問題是我在當前項目中做的很多常見模式:RXJava2:正確的模式鏈改造請求

您有一個請求,如果成功,您將使用該請求發出第二個請求。

如果要麼失敗,您需要能夠確定哪一個失敗。 (主要是爲了顯示自定義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將獲取實際的Token我需要在SecondRequest中。如果沒有令牌,則無法發出第二次請求。

回答

5

我會建議使用平面地圖(如果這是一個選項retrolambda)。 如果你沒有做任何事情,你也不需要保留返回值(例如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風格。如果可能的話,你希望鏈條成爲高層次行爲的列表,因此它可以被看作是一系列動作/轉換。

編輯 沒有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, 
      }       
    }); 
+0

感謝您的迴應,這是我的想法(不幸的是我沒有Retrolambda在這個項目上)你會介意幫助我解開lambda簡化嗎? –

+0

@MartinMarconcini我添加了一個沒有lambda的例子。 – cyroxis

+0

感謝您的示例,我得到了它的工作。只是爲了記錄這是RXJava2,所以'Func1'是'Function'。 –

1

這不適合你嗎?

retrofitService 
.getSomething() 
.flatMap(firstResponse -> retrofitService.getSecondResponse(firstResponse.id)) 
.doOnNext(secondResponse -> {/* both requests succeeded */}) 
/* do more stuff with the response, or just subscribe */ 
+0

它可以,但我會在哪裏找到firstResponse的錯誤? (另外,不幸的是,我可以在這個項目中進行retrolambda)。 –

+0

一旦你發現錯誤,你會怎麼做? –

+0

特別通知用戶界面(firstResponse是一個令牌,未能獲取它,需要用戶界面做出不同的反應)。如果令牌失效,主持人(負責此數據交互)將需要做不同的事情。第二次調用使用該令牌,並且將再次以不同的UI狀態結束。是否有意義? –