我可以使用GCD限制并发请求吗? [英] Can I limit concurrent requests using GCD?

查看:66
本文介绍了我可以使用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限制并发请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆