0

I have a function in repository that calls three APIs which return three different type of Objects.

fun scanSource(code: String) =
    firstApi.DataV11Get(code)
        .flatMap {
            firstApi.sccGet(it.sscc)
        }
        .flatMap {
            lastApi.lsccGet(it.id)
        }

currently it is only returning the result of last API in the ViewModel .Now I want to have result of all of these in viewmodel.

plus the api must be sequential as they are using the data from the last API.

Also each API returns a different Object type.And I want all of three objects once all three calls are finished.

Thanks

Syeda Zunaira
  • 5,191
  • 3
  • 38
  • 70

1 Answers1

1

Declare this funtion in your Api repo

   fun getJobs(contractor_id: String): Observable<JobListModel> {
    return Observable.zip(
        apiInterface.pendingJobs(contractor_id, 0, 5),
        apiInterface.activeJobs(contractor_id, 0, 5),
        apiInterface.completedJobs(contractor_id, 0, 5),
        apiInterface.cancelledJobs(contractor_id, 0, 5),
        Function4<PendingJobResponseModel, ActiveJobResponseModel, CompletedJobResponseModel, CancelledJobResponseModel, JobListModel> { p, a, c, ca ->
            JobListModel(p, a, c, ca)
        }
    )
}

This is the method to make zip to multiple requests and model.

Please refer this link for more detailed explanation Rxjava Android how to use the Zip operator

Job ListModel Class which maps all responses

class JobListModel(
val pendingJobResponseModel: PendingJobResponseModel,
val activeJobResponseModel: ActiveJobResponseModel,
val completedJobResponseModel: CompletedJobResponseModel,
val cancelledJobResponseModel: CancelledJobResponseModel) : Serializable
Afzal Khan
  • 336
  • 3
  • 16