1

How to get response of a nested api call using RxSwift and alamofire? Here i get a response from an alamofire api cal and with that result i need to call another api call. I want to get the second api call response. Can anyone suggest me a solution to solve this. please.

func origin() -> Observable<String> {
return Alamofire.request("httpbin.org/get").rx.responseJSON() 

}

func otherApiCall(with origin: String) -> Observable<YourType> {
// Other api call using origin
return Alamofire...........

}

then

origin()
.flatMap{ origin in 
    otherApiCall(with: origin)  
}
.subscribe(onNext: { response in 

})
.disposed(by: disposeBag)
gunjan
  • 21
  • 1
  • 6

1 Answers1

0

Your code is not explaining much but as per my understanding if you want to call api using other api's response than below code should work.

func firstRequest() -> Observable<firstRequestResponseType> {
    return firstRequest
}

firstRequest
.flatMap { (firstRequestResponseType) ->  Observable<secondRequestResponseType>
    return secondRequest
}
.map { (secondRequestResponseType)
 //you can user second request’s response here
}
.subscribe()
iOSGhost
  • 182
  • 2
  • 9
  • Actually the compiler didnt wait for the response of second call response – gunjan Feb 27 '18 at 10:07
  • @gunjan you will reach in map block after both API request will get complete – iOSGhost Feb 27 '18 at 15:06
  • actually the second api call depends upon a value from the response of first api call. And here i need to call the second api call with those first response values that satisfies the condition. So the second api call will be called any number of times .And i need to get the response to append with the first response in a looping manner..can you help me – gunjan Feb 28 '18 at 04:18
  • you can use first request's response in flatMap block {in above example} or if you want to share first request's response multiple times you can use share() method of RxSwift – iOSGhost Feb 28 '18 at 05:21