On NSRunLoop, clarification needed
-
26-10-2019 - |
Question
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.
Solution
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.