How to handle multiple network call in Alamofire

一个人想着一个人 提交于 2019-11-30 13:56:31

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.

DispatchGroup would be a good option to handle multiple dependent requests in parallel

func loadData() {
    let dispatchGroup = DispatchGroup()

    func startRequests() {
        dispatchGroup.enter()
        loadDataRequest1()
        dispatchGroup.enter()
        loadDataRequest2()

        dispatchGroup.notify(queue: .main) { [weak self] in
            // Process your responses
        }


        loadDataRequest1() {
            // Save your response
            dispatchGroup.leave()
        }

        loadDataRequest2() {
            // Save your response
            dispatchGroup.leave()
        }
    }

    startRequests()
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!