RxJava Zip无法正常工作

时间:2018-02-20 10:10:29

标签: kotlin zip retrofit2 rx-java2 flatmap

我正在尝试实现这种复杂的API结构。 API Structure

所以我尝试用RxJava2 zip实现并行请求

  private fun getDetails(marketDataQuotes: MarketDataQuotes, instrumentById: InstrumentById, subscribe: Subscribe): Observable<DetailsWatchListModel> {
    return Observable.zip(
            getMarketDataQutoes(marketDataQuotes),
            getInstrumentById(instrumentById),
            getSubscribeInstrument(subscribe),
            Function3<MarketDataQuotesResponse, List<InstrumentByIdResponse>, SubscribeResult,DetailsWatchListModel>
            { marketData, instrumentList, subscribeInstrument ->
                detailWatchList(marketData, instrumentList, subscribeInstrument)
            })

}

但面对此问题Error

private fun getSubscribeInstrument(subscribe: Subscribe): LiveData<SubscribeResult> {
    val mutableLiveData = MutableLiveData<SubscribeResult>()
    remoteServices.requestSubscribe(subscribe)
            .subscribeOn(Schedulers.io())
            //  .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(object : ErrorCallBack<BaseResponse<SubscribeResult>>() {
                override fun onSuccess(t: BaseResponse<SubscribeResult>) {
                    L.d("Success of Market data Quotes")
                    // mutableLiveData.value = transform(t)
                }

            })
    return mutableLiveData
}

其他API调用与单一位置错误处理和基本响应结构

类似

和服务一样

@Headers("Content-Type: application/json")
@POST("instruments/subscription")
fun requestSubscribe(@Body subscribe: Subscribe): Observable<BaseResponse<SubscribeResult>>

使用Kotlin v1.2.21,retofit 2.3.0,RxJava2 2.1.5请让我知道我做错了什么.. Tanx in Advance

1 个答案:

答案 0 :(得分:0)

LiveData无法使用RxJava压缩。

您需要使用此API:https://developer.android.com/reference/android/arch/lifecycle/LiveDataReactiveStreams.html

所以将LiveData转换为RxJava然后压缩它。