【发布时间】:2020-05-08 10:21:08
【问题描述】:
我想将 company 和 workFor 变量合并到一个流中,但我不知道如何。我尝试使用 switchMap、zip、merge 和 concatMapIterable,但没有任何效果。或者我做错了什么..
我的数据模型:
data class UserCompany(
@field:Json(name = "user") val user: User,
@field:Json(name = "company") val company: Company,
@field:Json(name = "workFor") val workFor: List<Company>
)
还有我当前的代码:
authApi.getCompany(getJwt()) //here I get data that looks like the above model
.subscribeOn(Schedulers.io())
.flatMapIterable {
return@flatMapIterable it.data.workFor.toMutableList().add(it.data.company) //error Type mismatch
},
//should returns stream of company and workFor
如何使用 Rx 组合来自一个请求的两个变量?
编辑,更好的例子。 假设我们有:
data class Pet(val name: String)
data class PetResult(
val myPet: Pet, //dog
val otherPet: List<Pet> //cat, bird, cow
)
我想得到这样的东西:
authApi.getPet() // response looks like PetResult (model above)
.subscribeOn(Schedulers.io())
.subscribe(
{ petList.setSuccess(it) }, // should returns dag, cat, bird, cow
{ petList.setError(it.message) }
)
那么,如何将 myPet 与我从一个 API 请求中获得的 otherPet 合并?
【问题讨论】:
标签: android rx-java rx-java2 rx-android