使用 Rx 在一个请求中组合两个变量

Combine two variables in one request using Rx

我想将 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 合并?

你可以像下面这样实现上面的例子:

    apiClient.getPet.flatMap {
        var getListResponse=it;  
        //From getListResponse you 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

    }