我想将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) }
)
因此,如何将我从一个API请求中获得的myPet与otherPet合并?
您可以实现上面的示例,如下所示
apiClient.getPet.flatMap {
var getListResponse=it;
//From getListResponse U will get Result obj which contain pet and other pet list
var pet=it.getPet()
var petList=it.getPetList()
petList.add(pet)
getListResponse.setPetList=petList
}