How to handle multiple network calls in Alamofire

I need to call 2 apis in the view controller to retrieve some data from the server, I want them to start at the same time, but the next step will only be started if both of them are returned (it doesn't matter if success or failure ) I can offer two solutions: 1. Chain them together. Call api1, call api2 in the api1 result handler, wait for the result api2 2. Set 2 indicators of the Bool indicator, create a check function, if both of these indicators are correct, do the following. In both Apis result handlers, set the appropriate indicator variable, then the call test function, to decide whether to go well

The first is not enough, and I cannot say that the second is an elegant solution. Does Alamofire have something like a combined signal in Reactivecocoa? Or any better solution?

+6
source share
1 answer

Your rating is 100% correct. At the moment, the two options that you have posted are really the only possible approaches. I agree with you that your second option is much better than the first of your use cases.

If you want to combine ReactiveCocoa with Alamofire, then this is certainly possible, but not yet done, as far as I know. You can also investigate if PromiseKit can provide some help, but it has not yet been glued together with Alamofire. Trying to combine any of these libraries with Alamofire answer serializers will not be any trivial task.

Shifting gears a bit, I really don't think ReactiveCocoa or PromiseKit is very good for your use case, since you are not binding service calls, you are running them in parallel. In addition, you still need to run all the parsing logic and determine whether it succeeded or failed, and then update the application. What I get is that option 2 would be your best bet if you don't want to go out of your way to combine PromiseKit or ReactiveCocoa with Alamofire answer serializers.

Here's what I would suggest making things less complicated.

import Foundation import Alamofire class ParallelServiceCaller { var firstServiceCallComplete = false var secondServiceCallComplete = false func startServiceCalls() { let firstRequest = Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["first": "request"]) firstRequest.responseString { request, response, dataString, error in self.firstServiceCallComplete = true self.handleServiceCallCompletion() } let secondRequest = Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["second": "request"]) secondRequest.responseString { request, response, dataString, error in self.secondServiceCallComplete = true self.handleServiceCallCompletion() } } private func handleServiceCallCompletion() { if self.firstServiceCallComplete && self.secondServiceCallComplete { // Handle the fact that you're finished } } } 

The implementation is really clean and easy to use. Although I understand your desire to get rid of completion flags and callback functions, other parameters, such as ReactiveCocoa and / or PromiseKit, will still have additional logic and may ultimately complicate the situation.

Another possible option is to use send groups and semaphores, but this does add complexity, but may come close to the ReactiveCocoa or PromiseKit approach.

I hope this helps shed some light.

+8
source

Source: https://habr.com/ru/post/983173/


All Articles