On NSRunLoop, clarification needed

不问归期 提交于 2019-12-11 02:48:08

问题


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];

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

[[NSRunLoop currentRunLoop]run];

I would have thought that startImmediately:YES would do exactly that.


回答1:


async callbacks require an NSRunLoop. See:

Cocoa: NSURLConnection not attempting an HTTP Request

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

From the docs: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

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

... 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.



来源:https://stackoverflow.com/questions/8336693/on-nsrunloop-clarification-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!