序列化NSURLConnection请求(iOS) - 使用同步请求? [英] Serializing NSURLConnection Requests (iOS) - Use Synchronous Request?

查看:133
本文介绍了序列化NSURLConnection请求(iOS) - 使用同步请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环查看日期列表,并向列表中的每个日期的网络服务器发出请求。

I'm looping through a list of dates and making a request to a web server for each date in the list.

我想要处理每个日期完全在将后续请求发送到服务器之前。为此,我已经使用GCD设置了一个串行调度队列。每次通过日期循环,一个块被添加到队列。

I would like each date to be processed completely before the subsequent request is sent to the server. To do this, I have set up a serial dispatch queue using GCD. Each time through the date loop, a block is added to the queue.

我遇到的问题是,我的NSURLConnection是使用标准异步调用设置。这导致请求不阻塞任何后续请求。

The problem I am having is that my NSURLConnection is set up using the standard asynchronous call. This results in requests not blocking any subsequent requests. They are thus overrunning each other.

我的问题:这是一种情况,我可以使用同步 NSURLConnection调度队列)还是有其他方法使它工作使用标准的异步调用?

My question: Is this a case where it would make sense for me to use the synchronous NSURLConnection (within the dispatch queue) or is there some other way to make it work using the standard asynchronous call?

推荐答案

有很多方法可以做到这一点。无论您选择什么方法,启动连接都需要绑定到完成处理任务。

There are number of ways to do this. Whatever method you choose, starting the connection needs to be tied to completion of your processing task.


  1. 您的串行队列,使用同步请求。这可能是给定您当前实现的最快解决方案,只要您对同步请求的有限错误处理确定即可。

  1. In each block you add to your serial queue, use a synchronous request. This is probably the quickest solution given your current implementation as long as you're ok with the limited error handling of a synchronous request.

串行队列。启动第一个异步连接并处理响应。当处理完成时,启动下一个异步连接。冲洗并重复。

Don't use a serial queue. Start the first asynchronous connection and process the response. When processing is complete start the next asynchronous connection. Rinse and repeat.

这篇关于序列化NSURLConnection请求(iOS) - 使用同步请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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