标签:请求 one rms start 操作 epo thread 问题 log
主线程中创建一个NSURLConnection并异步运行
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES]; - (void)start { //step 1:请求地址 NSURL *url = [NSURL URLWithString:@"www.2cto.com"]; //step 2:实例化一个request NSURLRequest *request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; //step 3:创建链接 self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];//直接执行了 if (self.connection) { NSLog(@"链接成功"); }else { NSLog(@"链接失败"); } }
解决方法,改动代码为:
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES]; - (void)start { //step 1:请求地址 NSURL *url = [NSURL URLWithString:@"www.2cto.com"]; //step 2:实例化一个request NSURLRequest *request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; //step 3:创建链接 self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];//临时不执行 [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];//用NSRunLoopCommonModes [connection start]; if (self.connection) { NSLog(@"链接成功"); }else { NSLog(@"链接失败"); } }
标签:请求 one rms start 操作 epo thread 问题 log
原文地址:http://www.cnblogs.com/claireyuancy/p/6916333.html