2017-02-14 10 views
4

Observableを返すネットワークコールがあり、最初にObservableに依存するrxではない別のネットワークコールがあります。何とかRxでそれをすべて変換します。私はObservableを返さない別のHTTP呼び出しを行う必要があります実行した後rxネットワークコールに依存する非rxネットワークコールを呼び出す方法

Observable<Response> responseObservable = apiclient.executeRequest(request); 

responseObservable.map(response - > execute the no rx network call using the response.id) 

noRxClient.getInformation(response.id, new Action1<Information>() { 
    @Override 
    public void call(Information information) { 
     //Need to return information with page response 
    } 
}); 

この後、私は応答どう

renderResponse(response, information); 

をレンダリングするために、このメソッドを呼び出す必要があります非rx呼び出しをrxに接続して、RxJavaでレンダリング応答をすべて呼び出すことはできますか?あなたの非同期非RXをラップすることができます

答えて

2

は(非非同期呼び出しのため)Observable.fromEmitter(RxJava1)を使用してObservableまたはObservable.create(RxJava2)とObservable.fromCallableに呼び出します

private Observable<Information> wrapGetInformation(String responseId) { 
    return Observable.create(emitter -> { 
     noRxClient.getInformation(responseId, new Action1<Information>() { 
      @Override 
      public void call(Information information) { 
       emitter.onNext(information); 
       emitter.onComplete(); 
       //also wrap exceptions into emitter.onError(Throwable) 
      } 
     }); 
    }); 
} 

private Observalbe<RenderedResponse> wrapRenderResponse(Response response, Information information) { 
    return Observable.fromCallable(() -> { 
     return renderResponse(response, information); 
     //exceptions automatically wrapped 
    }); 
} 

と組み合わせた結果overloaded flatMap演算子を使用して:

apiclient.executeRequest(request) 
    .flatMap(response -> wrapGetInformation(response.id), 
      (response, information) -> wrapRenderResponse(response, information)) 
    ) 
    //apply Schedulers 
    .subscribe(...) 
+0

wrapRenderResponseから何も返されない場合はどうなりますか?それはただ応答をレンダリングします。そのコードはどのように変更できますか? –

関連する問題