当我
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会这么做。
发布于 2011-12-01 13:03:44
异步回调需要NSRunLoop。请参见:
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
initWithRequest:delegate:返回初始化的URL连接并开始加载URL请求的数据。
- (id)initWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate...为了使连接正常工作,调用线程的run循环必须在默认的run循环模式下运行。参见scheduleInRunLoop:forMode:更改run循环和模式。
https://stackoverflow.com/questions/8336693
复制相似问题