Answer the question
In order to leave comments, you need to log in
How to make two Rx requests into one using flatMap?
I have the following data class:
data class MediaDetail(
val dateCreated:String,
val nasaId:String,
val keywords:List<String>,
val mediaType:String,
val center:String,
val title:String,
val description:String,
val location:String,
var assets:Map<String,String>?
)
fun fetchMediaDetails(nasaId:String){
_networkState.postValue(NetworkState.LOADING)
try {
compositeDisposable.add(
apiService.mediaInfo(nasaId)
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.io())
.subscribe ({
fetchMediaAsset(it.item)
},{
_networkState.postValue(NetworkState.ERROR)
Log.e("MediaDetailsDataSource", it.message.toString())
})
)
}
catch (e: Exception){
Log.e("MediaDetailsDataSource", e.message.toString())
}
}
private fun fetchMediaAsset(mediaDetail: MediaDetail){
try {
compositeDisposable.add(
apiService.mediaAsset(mediaDetail.nasaId)
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.io())
.subscribe({
mediaDetail.assets = it.item
_downloadedMediaDetailsResponse.postValue(mediaDetail)
_networkState.postValue(NetworkState.LOADED)
}, {
Log.e("MediaDetailsDataSource", it.message.toString())
})
)
}
catch (e: Exception){
Log.e("MediaDetailsDataSource", e.message.toString())
}
}
Answer the question
In order to leave comments, you need to log in
So do it.
request1.flatMap{ mediaDetail ->
request2(mediaDetail.id).map{
mediaDetail.copy(assets=it.item)
}
}. subscribe
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question