How to handle multiple network call in Alamofire

前端 未结 2 1492
天命终不由人
天命终不由人 2021-01-02 16:42

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 re

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 17:10

    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.

提交回复
热议问题