Objective c 使用NSR操作的异步NSURL连接
我想在后台模式下进行Objective c 使用NSR操作的异步NSURL连接,objective-c,ios,nsurlconnection,nsoperation,nsinvocation,Objective C,Ios,Nsurlconnection,Nsoperation,Nsinvocation,我想在后台模式下进行NSURLConnection,因为它的响应有很多数据。论坛告诉我在dienterbackground中使用苹果的有限长度编码。 但是我想避免它。我通过NSOperation和NSInvocationas使用下面的代码来代替它,但是它不工作。connectToServer正在进行nsurconnection操作。请问有什么帮助吗?didReceiveData,didReceiveResponse未调用委托方法 NSOperationQueue *queue = [NSOpe
NSURLConnection
,因为它的响应有很多数据。论坛告诉我在dienterbackground
中使用苹果的有限长度编码。
但是我想避免它。我通过NSOperation
和NSInvocation
as使用下面的代码来代替它,但是它不工作。connectToServer
正在进行nsurconnection
操作。请问有什么帮助吗?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");
}
}
查看sendAsynchronousRequest:queue:completionHandler:method。它允许使用NSURLConnection进行异步请求
注意,这需要iOS5或更高版本这类问题被问了无数次。代理未被调用,因为从iOS 4开始,操作是在辅助线程上启动的。线程可能在调用代理之前退出,仅此而已 在主线程上保持连接,并使用GCD在后台线程中处理数据 我在这里写了这些东西:
编辑:更新链接。苹果提供了
QHTTPOperation
,它可以满足您的需求,将nsurconnection
封装在NSOperation
中,只需一个请求。你可以在苹果的网站上找到它
QHTTPOperation
实际上是QRunLoopOperation
的一个子类,它允许您封装依赖于NSO操作中的运行循环回调的逻辑
第三方
ASIHTTPRequest
是一个类似的流行解决方案,尽管它不再被维护。我的第一个链接是针对Mac的,我已经为iOS版本更新了它。你想要Mac参考资料吗?Mac文档可以在这里找到:我想在iOS4中执行。请回答我的问题,为什么它不工作?请查看我编辑的问题?享受我辛苦赢得的50票。你应该在我的赏金之前回答:)你能告诉我如何停止Runloop和端口吗?[Runloop removePort:port forMode:MODE];CFRunLoopStop(CFRunLoopGetCurrent());非常感谢@Nyx0uf,你为我节省了很多时间。。。!伟大的贡献!我不认为这是一个解决方案,答案是一个变通办法,但不是问题的解决方案。