iPhone 上的同步 NSURLConnection 线程

发布于 2024-08-16 10:34:20 字数 127 浏览 2 评论 0原文

我一开始使用异步,但获取返回的数据变得很麻烦。然后我开始使用类方法,我相信这排除了使用委托方法的可能性。所以我继续使用同步,知道这将是一个问题,但不认为我会在解决方案上遇到这么大的困难。在保持 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你与清晨阳光 2024-08-23 10:34:20

同步 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/

开始看清了 2024-08-23 10:34:20

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.

国际总奸 2024-08-23 10:34:20

出于兴趣,您在获取数据时遇到了什么问题?我发现使用异步 NSURLConnection 和 NSNotificaion 完成后是一个非常方便的解决方案。

在您需要数据的类中(我通常将其放在 init 方法中)

-(id)init{
    ... object setup ...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView) name:@"ScanCompleted" object:nil];
}

updateView 是当连接完成接收

NSURLConnection 中的

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

//Notify that we are finished
[[NSNotificationCenter defaultCenter] postNotificationName:soapAction object:self];

}

数据时要调用的方法这对我来说非常有效 - 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)

-(id)init{
    ... object setup ...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView) name:@"ScanCompleted" object:nil];
}

updateView is the method to be called when the connection has finished receiving data

in the NSURLConnection

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

//Notify that we are finished
[[NSNotificationCenter defaultCenter] postNotificationName:soapAction object:self];

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文