2011-12-01 202 views
2

当我在NSRunLoop,澄清需要

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会做到这一点。

+0

这段代码是在单独的线程还是主线程中运行? –

+0

主线我知道 – JAM

+0

你在命令行应用程序或gui应用程序? – bryanmac

回答

4

异步回调需要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:委托: 返回初始化的URL连接,并开始加载数据的URL请求。

- (id)initWithRequest:(NSURLRequest *)request delegate:(id <NSURLConnectionDelegate>)delegate 

...用于连接才能正常工作,调用线程的run 循环必须在默认的运行循环模式下运行。请参阅 scheduleInRunLoop:forMode:更改运行循环和模式。

+0

Bryan - 你的意思是说GUI应用程序自动获取'NSRunLoop',而命令行应用程序需要明确声明它? – JAM

+0

就是这样。 – Wevah

+0

谢谢先生。确实很有帮助。 – JAM