iPhone 上的同步 NSURLConnection 线程
我一开始使用异步,但获取返回的数据变得很麻烦。然后我开始使用类方法,我相信这排除了使用委托方法的可能性。所以我继续使用同步,知道这将是一个问题,但不认为我会在解决方案上遇到这么大的困难。在保持 UI 可交互性的同时检索数据的最佳方法是什么?
I started out using asynchronism but to get the returned data became a hassle. I then began using class methods which, I believe, ruled out the possibility of using the delegate methods. So I kept with the synchronous, knowing that this would be an issue but didn't think I would have this much difficulty with a solution. What is the best way to retrieve data whilst keeping the UI interact-able?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
同步 NSURLConnections 在 NSOperation 内部工作得很好 - 并且因为您同步使用它们,所以 NSOperationQueue 传递给 NSOperations 将自动使用后台线程来运行它们(否则您必须做额外的工作)。
让 URL 连接在后台线程中运行是保持 UI 响应的关键,而异步 NSURLOperation 默认情况下不会执行此操作(查看数据回调全部进入哪个线程)。
有时您想要处理数据传入(例如,如果您有进度条),在这些情况下,异步 URL 连接更好,它们仍然可以位于 NSOperation 中。
因为这让你感到沮丧,你可能很想看看替代的 ASIHTTPRequest 库,它也基于 NSOperation,但似乎它可能会让你不那么痛苦(他们有很好的示例代码):
http://allseeing-i.com/ASIHTTPRequest/
Synchronous NSURLConnections work great inside of an NSOperation - and because you are using them synchronously, an NSOperationQueue handed the NSOperations will automatically use a background thread to run them (you have to do extra work otherwise).
Having URL connections operate in a background thread is the key to keeping the UI responsive, something that an asynchronous NSURLOperation does not do by default (look at what thread the data callbacks all come in on).
Sometimes you want to handle data incoming (like if you have a progress bar) and in those cases asynchronous URL connections are better, they can still be in an NSOperation.
Because this is frustrating you, you may well want to take a look at the alternative ASIHTTPRequest library, also based on NSOperation but it seems like it may make some of this less painful for you (they have good example code):
http://allseeing-i.com/ASIHTTPRequest/
NSURLConnection 同步调用应该能够卡在后台线程中,但我强烈建议您处理“麻烦”,并以正确的方式进行。在保持 UI 交互的同时检索数据的最佳方法是使用 NSURLConnection 的异步方法。
The NSURLConnection synchronous call should be able to be stuck in a background thread, but I strongly recommend you deal with the 'hassle', and do it The Right Way™. The best way to retrieve data whilst keeping the UI interactive is to use NSURLConnection's asynchronous methods.
出于兴趣,您在获取数据时遇到了什么问题?我发现使用异步 NSURLConnection 和 NSNotificaion 完成后是一个非常方便的解决方案。
在您需要数据的类中(我通常将其放在 init 方法中)
updateView 是当连接完成接收
NSURLConnection 中的
数据时要调用的方法这对我来说非常有效 - NSURLConnection 是线程化的,因此 UI 不会不会锁定,数据下载完成后会更新。
Out of interest what issues where you having with getting the data? I found using async NSURLConnection and a NSNotificaion when it had finished to be quite a handy solution.
In the class where you need the data (I normally put it in the init method)
updateView is the method to be called when the connection has finished receiving data
in the NSURLConnection
This works for me quite nicely - the NSURLConnection is threaded so the UI doesn't lock up and it gets updated when the data has finished downloading.