具有NSOperation的异步NSURLConnection [英] Asynchronous NSURLConnection with NSOperation
问题描述
我想在后台模式下执行 NSURLConnection
,因为它的响应有很多数据。论坛要求使用Apple的有限长度编码在 didEnterBackground
。
但是我想避免它。而不是它我使用以下代码通过 NSOperation
与 NSInvocation
一样,但它没有用。 connectToServer
有 NSURLConnection
operation.any help please? didReceiveData
, didReceiveResponse
不调用委托方法?
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?
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");
}
}
推荐答案
这有点问题被问了很多时间。代理人没有被调用,因为从iOS 4开始,操作是在辅助线程上启动的。线程可能会在代理被调用之前退出。
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.
保持主线程上的连接并使用GCD处理后台线程中的数据。
Keep the connection on the main thread and handle the data in a background thread using GCD.
我在这里写了所有这些内容: http://cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/
I'v wrote about all this stuff here : http://cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/
编辑:更新链接。
这篇关于具有NSOperation的异步NSURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!