简体   繁体   English

Swift 中使用 Alamofire 的多个异步请求完成处理程序

[英]Multiple async requests completion handler in Swift with Alamofire

I'm building my app with Swift and I'm using Alamofire for my web requests.我正在使用 Swift 构建我的应用程序,我正在使用 Alamofire 来处理我的网络请求。 I need to perform 4 requests at the same time and then run a function only when information is downloaded.我需要同时执行 4 个请求,然后仅在下载信息时运行一个函数。 How can I do that?我怎样才能做到这一点? Thank you!谢谢!

You can use dispatch_group_async and dispatch_group_notify .您可以使用dispatch_group_asyncdispatch_group_notify Create group of requests with dispatch_group_async and dispatch_group_notify will notify you once all the requests are complete.创建请求组dispatch_group_asyncdispatch_group_notify会立即通知您所有的要求是完整的。

An easy way to do this would be to have a variable you would lock (or not if it's an atomic variable) you would increment after each request is executed.一个简单的方法是有一个你会锁定的变量(或者如果它是一个原子变量,你会在每个请求执行后递增)。 If it reaches 4 once you increment it, execute your function.如果它在增加后达到 4,则执行您的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM