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);
}
24.NSURLConnection与RunLoop
最新推荐文章于 2022-03-14 09:07:34 发布