nam*_*ess 14 objective-c nsurlconnection nsoperation nsinvocation ios
我想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");
}
}
Run Code Online (Sandbox Code Playgroud)
Nyx*_*0uf 42
这个问题被问了很多次.代理没有被调用,因为从iOS 4开始,操作是在辅助线程上启动的.线程可能会在代理被调用之前退出.
保持主线程的连接,并使用GCD在后台线程中处理数据.
我在这里写了所有这些东西:http://cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/
编辑:更新链接.
| 归档时间: |
|
| 查看次数: |
20037 次 |
| 最近记录: |