简体   繁体   English

iPhone上的同步NSURLConnection线程

[英]Synchronous NSURLConnection Threading on iPhone

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? 在保持UI交互性的同时检索数据的最佳方法是什么?

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™. NSURLConnection同步调用应该能够被卡在后台线程中,但是我强烈建议您处理“麻烦”,然后执行“正确的方式”。 The best way to retrieve data whilst keeping the UI interactive is to use NSURLConnection's asynchronous methods. 在保持UI交互性的同时检索数据的最佳方法是使用NSURLConnection的异步方法。

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). 同步NSURLConnections在NSOperation内部非常有效-并且因为您正在同步使用它们,所以递给NSOperations的NSOperationQueue将自动使用后台线程来运行它们(否则,您必须做额外的工作)。

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). 使URL连接在后台线程中运行是保持UI响应能力的关键,这是异步NSURLOperation默认情况下不执行的操作(请查看所有数据回调都位于哪个线程上)。

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. 有时您想处理传入的数据(例如,如果有进度条),在这种情况下,异步URL连接更好,它们仍然可以处于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): 因为这让您感到沮丧,所以您可能想看看同样基于NSOperation的替代ASIHTTPRequest库,但是看起来这可能会使其中的一些麻烦(他们有很好的示例代码):

http://allseeing-i.com/ASIHTTPRequest/ http://allseeing-i.com/ASIHTTPRequest/

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. 我发现使用异步NSURLConnection和NSNotificaion可以解决问题,非常方便。

In the class where you need the data (I normally put it in the init method) 在需要数据的类中(我通常将其放在init方法中)

-(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 updateView是连接完成接收数据后要调用的方法

in the NSURLConnection 在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. 这对我来说很好用-NSURLConnection是线程化的,因此UI不会锁定,并且在数据下载完成后会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM