在NSRunLoop上,需要进行澄清 [英] On NSRunLoop, clarification needed

查看:132
本文介绍了在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];

...没有任何反应。直到i

... nothing happens. Delegate methods are not called until i

[[NSRunLoop currentRunLoop]run];

我以为 startImmediately:YES 会做到这一点

推荐答案

异步回调需要一个NSRunLoop。见:

async callbacks require an NSRunLoop. See:

可可:NSURLConnection不尝试HTTP请求

命令行应用程序默认情况下没有NSRunLoop - gui apps do。

command line apps don't have an NSRunLoop by default - gui apps do.

从文档中: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆