ios 带有 NSOperation 的异步 NSURLConnection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9223537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asynchronous NSURLConnection with NSOperation
提问by nameless
I want to do NSURLConnection
in background mode,because it response is having much data.Forums are telling to use Apple's finite length coding to use in didEnterBackground
.
but I want to avoid it.instead of it I use following code through NSOperation
with NSInvocation
as, but it is not working.connectToServer
is having NSURLConnection
operation.any help please?didReceiveData
,didReceiveResponse
delegate methods are not called?
我想NSURLConnection
在后台模式下做,因为它的响应有很多数据。论坛告诉使用 Apple 的有限长度编码在didEnterBackground
. 但我想避免。相反的是我通过使用下面的代码NSOperation
用NSInvocation
的,但它不工作。connectToServer
正在NSURLConnection
手术。请帮忙吗?didReceiveData
,didReceiveResponse
委托方法没有被调用?
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(connectToServer)
object:nil];
[queue addOperation:operation];
[operation release];
[queue autorelease];
-(void)connectToServer
{
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
回答by Nyx0uf
This kind of question was asked a zillion time. Delegates are not getting called because as of iOS 4 operations are started on a secondary thread. The thread probably exits before the delegates are called that's all.
这种问题被问了无数次。由于 iOS 4 操作是在辅助线程上启动的,因此不会调用委托。线程可能在调用委托之前退出,仅此而已。
Keep the connection on the main thread and handle the data in a background thread using GCD.
保持主线程上的连接并使用 GCD 在后台线程中处理数据。
I'v wrote about all this stuff here : http://cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/
我在这里写了所有这些东西:http: //cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/
EDIT : Updated link.
编辑:更新链接。
回答by Danra
Apple provides QHTTPOperation
, which does just what you want, encapsulates an NSURLConnection
within an NSOperation
, for a single request. You can find it in Apple's sample code.
Apple 提供了QHTTPOperation
,它可以满足您的要求,将一个 , 封装NSURLConnection
在 , 中NSOperation
,用于单个请求。您可以在 Apple 的示例代码中找到它。
QHTTPOperation
is actually a subclass of QRunLoopOperation
which lets you encapsulate logic which depends on run-loop callbacks in an NSOperation.
QHTTPOperation
实际上是它的一个子类,QRunLoopOperation
它允许您封装依赖于 NSOperation 中运行循环回调的逻辑。
The 3rd party ASIHTTPRequest
is a similar popular solution, AFAIK it is no longer maintained though.
第 3 方ASIHTTPRequest
是一个类似的流行解决方案,但 AFAIK 它不再被维护。
回答by Barlow Tucker
Look at the sendAsynchronousRequest:queue:completionHandler: method documented here in Apple's Documentation. It allows asynchronous requests with NSURLConnection.
查看Apple 文档中记录的 sendAsynchronousRequest:queue:completionHandler: 方法。它允许使用 NSURLConnection 进行异步请求。
Note this requires iOS5 or higher
请注意,这需要 iOS5 或更高版本