如何在Alamofire中处理多个网络通话

编程入门 行业动态 更新时间:2024-10-11 11:15:16
本文介绍了如何在Alamofire中处理多个网络通话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在视图控制器中调用2个api来从服务器中获取一些数据,我希望它们同时启动,但是仅当两个函数都返回时,才会触发下一步(无论是成功或失败)。 我可以提出两种解决方案: 1.将它们链接在一起。调用api1,在api1的结果处理程序中调用api2,等待api2的结果2。设置2个Bool指标变量,创建一个检查函数,如果这两个指标都为true,则执行下一步。在两个Apis结果处理程序中,都设置了相应的指标变量,然后调用检查功能来确定是否可行

第一个还不够,我不能说第二个是一个优雅的解决方案。 Alamofire在Reactivecocoa中是否有类似合并信号的内容?还是有更好的解决方案?

解决方案

您的评估是100%正确的。目前,您列出的两个选项实际上是唯一可能的方法。我同意您的看法,在您的用例中,您的第二个选择要比第一个好得多。据我所知尚未完成。您还可以调查PromiseKit是否能够提供一些帮助,但是它也尚未与Alamofire结合在一起。试图将这两个库中的任何一个与Alamofire响应序列化程序结合起来,无论如何都不是一件容易的事。

稍微切换一下齿轮,我真的不认为ReactiveCocoa或PromiseKit非常适合您的用例,因为您没有链接服务调用,而是并行运行它们。此外,您仍然需要运行所有解析逻辑,并确定每个解析成功还是失败,然后相应地更新应用程序。我要说的是,除非您想将PromiseKit或ReactiveCocoa与Alamofire的响应序列化程序结合在一起,否则选择2将是迄今为止最好的选择。

这是我建议减少复杂程度的建议。

import Foundation import Alamofire 类ParallelServiceCaller { var firstServiceCallComplete = false var secondServiceCallComplete = false func startServiceCalls(){ let firstRequest = Alamofire.request(.GET, httpbin/get,参数:[ first: request]) firstRequest.responseString {请求,响应,dataString,中的错误self.firstServiceCallComplete = true self.handleServiceCallCompletion()} let secondRequest = Alamofire.request(.GET, httpbin/get,参数:[ second: request]) secondRequest.responseString { self.secondServiceCallComplete =中的请求,响应,dataString,错误= true self.handleServiceCallCompletion()} } 私有函数handleServiceCallCompletion() {如果self.firstServiceCallComplete& self.secondServiceCallComplete { //处理您已经完成的事实} } }

该实现非常干净而且易于遵循。虽然我了解您希望摆脱完成标志和回调函数的渴望,但其他选项(例如ReactiveCocoa和/或PromiseKit)仍将具有其他逻辑,并且最终可能会使事情变得更复杂。

另一种可能的选择是使用调度组和信号量,但这确实增加了复杂性,但会使您更接近ReactiveCocoa或PromiseKit风格的方法。

我希望这有助于您有所了解。

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?

解决方案

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, "httpbin/get", parameters: ["first": "request"]) firstRequest.responseString { request, response, dataString, error in self.firstServiceCallComplete = true self.handleServiceCallCompletion() } let secondRequest = Alamofire.request(.GET, "httpbin/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.

更多推荐

如何在Alamofire中处理多个网络通话

本文发布于:2023-11-26 02:15:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632243.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何在   网络   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!