使用委托时,使用超时处理NSURLRequest [英] Handling a NSURLRequest with timeout when using delegate

查看:123
本文介绍了使用委托时,使用超时处理NSURLRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSMutableURLRequest连接到网站以获取JSON响应。我这样做是通过生成请求,然后使用NSURLConnection与self的委托来处理异步连接。我已经实现了所有正常的委托方法:

I'm using a NSMutableURLRequest to connect to a web site to get a JSON response. I'm doing so by generating the request, and then using NSURLConnection with a delegate of self to handle the async connection. I've implemented all the normal delegate methods:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection

所有这些都可以很好地恢复数据,处理发生的任何错误等。但是,如果将NSURLRequest上的超时设置为10秒,我就不会似乎在这些方法中被调用(我希望调用didFailWithError)。它只是永远坐在那里,我没有通知它超时。

All works perfectly with getting data back, dealing with any errors that occur, etc. However with the timeout on the NSURLRequest set to 10 seconds I don't seem to get called in those methods (I expected the didFailWithError to get called). It just sits there forever and I have no notification that it timed out.

阅读iPhone API文档我没有看到任何提及应该在超时发生时发生,只是你可以设置超时值。应该怎么办?我没有在API中看到任何其他有用的委托方法。我不必手动检查超时,是吗?超时仅适用于同步请求吗?

Reading the iPhone API docs I don't see any mention of what should happen when the timeout occurs, just that you can set the timeout value. What should happen? I didn't see any other delegate methods in the API that would help. I don't have to manually check for timeouts, do I? Are timeouts only for synchronous requests?

推荐答案

此方法收到您的超时:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

这篇关于使用委托时,使用超时处理NSURLRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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