使用委托时,使用超时处理NSURLRequest

编程入门 行业动态 更新时间:2024-10-10 23:24:04
本文介绍了使用委托时,使用超时处理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

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

发布评论

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

>www.elefans.com

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