我可以使用GCD限制并发请求吗?

编程入门 行业动态 更新时间:2024-10-07 19:29:17
本文介绍了我可以使用GCD限制并发请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在从数据库异步获取数据.有什么方法可以将并发请求限制为一个数字,但仍然执行其余请求?

I'm acquiring data from a database asynchronously. Is there any way I can limit the concurrent requests to a number, but still execute the rest?

我看到了使用NSOperation和NSOperationQueue的帖子 但这对我来说太复杂了. 还有其他方法吗? GCD可以实现吗?

I saw a post using NSOperation and NSOperationQueue but this is too complicated for me. Is there any other ways to do it? Can GCD achieve it?

推荐答案

类似以下内容:

... //only make one of these obviously :) remaking it each time you dispatch_async wouldn't limit anything dispatch_semaphore_t concurrencyLimitingSemaphore = dispatch_semaphore_create(limit); ... //do this part once per task, for example in a loop dispatch_semaphore_wait(concurrencyLimitingSemaphore, DISPATCH_TIME_FOREVER); dispatch_async(someConcurrentQueue, ^{ /* work goes here */ dispatch_semaphore_signal(concurrencyLimitingSemaphore); }

更多推荐

我可以使用GCD限制并发请求吗?

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

发布评论

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

>www.elefans.com

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