Search code examples
swiftalamofire

How to handle multiple network call in Alamofire


I need to call 2 apis in a view controller to fetch some data from server, I want them to start at the same time, but next step will only be triggered if both of them are returned(doesn't matter it's a success or failure). I can come up with 2 solutions : 1. Chain them together. Call api1, call api2 in api1's result handler, wait for api2's result 2. Set 2 Bool indicator variables, create a check function, if both of these indicators are true, do next. In both Apis result handler, set corresponding indicator variable, then call check function to decide if it's good to go

First one is not sufficient enough, and I can't say the second one is a elegant solution. Does Alamofire has something like combine signal in Reactivecocoa? Or any better solution?


Solution

  • Your assessment is 100% correct. At the moment, the two options you laid out are really the only possible approaches. I agree with you that your second option is much better than the first given your use case.

    If you wish to combine ReactiveCocoa with Alamofire, then that's certainly possible, but hasn't been done yet to my knowledge. You could also investigate whether PromiseKit would be able to offer some assistance, but it hasn't been glued together with Alamofire yet either. Trying to combine either of these libraries with the Alamofire response serializers will not be a trivial task by any means.

    Switching gears a bit, I don't really think ReactiveCocoa or PromiseKit are very well suited for your use case since you aren't chaining service calls, you are running them in parallel. Additionally, you still need to run all your parsing logic and determine whether each one succeeded or failed and then update your application accordingly. What I'm getting at is that Option 2 is going to be your best bet by far unless you want to go to all the effort of combining PromiseKit or ReactiveCocoa with Alamofire's response serializers.

    Here's what I would suggest to keep 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 simple to follow. While I understand your desire to get rid of the completion flags and callback function, the other options such as ReactiveCocoa and/or PromiseKit are still going to have additional logic as well and may end up making things more complicated.

    Another possible option is to use dispatch groups and semaphores, but that really adds complexity, but could get you much closer to a ReactiveCocoa or PromiseKit styled approach.

    I hope that helps shed some light.