When i
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];
... 什么都没发生。委托方法不会被调用,直到我
[[NSRunLoop currentRunLoop]run];
我本以为startImmediately:YES
就会这么做。
异步回调需要 NSRunLoop。看:
Cocoa:NSURLConnection 未尝试 HTTP 请求 https://stackoverflow.com/questions/267163/cocoa-nsurlconnection-not-attempting-an-http-request
默认情况下,命令行应用程序没有 NSRunLoop - gui 应用程序有。
来自文档:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
initWithRequest:委托:
返回一个初始化的 URL 连接并开始加载 URL 请求的数据。
- (id)initWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate
...为了使连接正常工作,调用线程需要运行
循环必须在默认的运行循环模式下运行。看
ScheduleInRunLoop:forMode:更改运行循环和模式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)