如何可靠地管理后台请求?
我需要对 http 服务器异步运行查询并处理响应。该查询实际上是一个相当大的带有回调的方法调用链。这增加了复杂性,因为调用链中有很多返回路径。
在当前请求完全完成之前,不应启动新请求。
概要如下:
- 某些线程调用
[Foo poll]
。 poll
方法启动几个在后台运行的 HTTP 请求。在本例中为 ASIHTTPRequest。- 请求最终调用
[Foo onRequestComplete]
来解析响应。发生错误时,调用[Foo onRequestError
]。这是第二条返回路径。 - 然后,通过回调和更多返回路径对服务器进行更多调用。
- 最终将一些东西保存到磁盘上。
- 轮询已完成
这里有一个要点:在这些步骤中,对 poll
的调用应被忽略(返回)或阻止,直到轮询完成。
如何确保在另一个轮询运行时轮询
功能被阻止或无操作?
I need to asynchronously run a query against a http server and handle the response. The query is really a pretty large chain of method calls with callbacks. This adds complexity because there are a lot of return paths during the chain of calls.
It should not be possible to start a new request until the current one is completely completed.
Here is the general outline:
- Some thread calls
[Foo poll]
. - The
poll
method starts a couple of HTTP requests which runs in the background. In this case ASIHTTPRequest. - The requests eventually calls
[Foo onRequestComplete]
which parses the response. On error[Foo onRequestError
] is called. This is a second return path. - Then more calls are done to the server, with callbacks and more return paths.
- Eventually saving some stuff to disk.
- The poll is complete
Here is the catch: During these steps, calls to poll
should be ignored (returned) or blocked until the poll is complete.
How can you make sure the poll
function blocks or no-ops while another poll is running?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用 NSLock 即可。
在
[Foo poll]
中:在
[Foo onRequestComplete]
中:Simply use
NSLock
.In
[Foo poll]
:In
[Foo onRequestComplete]
: