24.NSURLConnection与RunLoop

来源:互联网 发布:阿里云学校应用场景 编辑:程序博客网 时间:2024/05/14 17:13
NSURLConnection *conn = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"qq"]] delegate:self];默认会开启一个子线程请求数据如果把请求放在 `dispatch_async` 中代理无返回数据    dispatch_async(dispatch_get_global_queue(0, 0), ^{NSURLConnection *conn = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"qq"]] delegate:self];// 决定代理方法在哪个队列中执行(如要代理中有返回需要设置)[conn setDelegateQueue:[[NSOperationQueue alloc] init]];// 启动子线程的runLoop(这种方式启动线程无法关闭)// [[NSRunLoop currentRunLoop] run];elf.runLoop = CFRunLoopGetCurrent();// 启动runLoop(这种方式启动线程可以手动关闭)CFRunLoopRun();});//NSURLConnectionDataDelegate- (void)connectionDidFinishLoading:(NSURLConnection *)connection{   // 停止RunLoop   CFRunLoopStop(self.runLoop);}
0 0