在NSRunLoop上,需要进行澄清 [英] On NSRunLoop, clarification needed
问题描述
当我
Logger *logger = [Logger new];
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
__unused NSURLConnection *conn = [[NSURLConnection alloc]
initWithRequest:request delegate:logger startImmediately:YES];
...没有任何反应。直到i
... nothing happens. Delegate methods are not called until i
[[NSRunLoop currentRunLoop]run];
我以为 startImmediately:YES
会做到这一点
推荐答案
异步回调需要一个NSRunLoop。见:
async callbacks require an NSRunLoop. See:
命令行应用程序默认情况下没有NSRunLoop - gui apps do。
command line apps don't have an NSRunLoop by default - gui apps do.
initWithRequest:delegate:
返回初始化的URL连接,并开始加载URL请求的数据。
initWithRequest:delegate: Returns an initialized URL connection and begins to load the data for the URL request.
- (id)initWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate
...为了使连接正常工作,调用线程的运行
循环必须在默认运行循环模式下运行。请参阅
scheduleInRunLoop:forMode:更改运行循环和模式。
... for the connection to work correctly, the calling thread’s run loop must be operating in the default run loop mode. See scheduleInRunLoop:forMode: to change the run loop and mode.
这篇关于在NSRunLoop上,需要进行澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!