objective-ciosmultithreadingcfrunloop

iOS: CFRunLoopRun() function confusion


I've read about CFRunLoop but still a bit confused about it. I came a cross a piece of code that I'd like to clarify for myself:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:url]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[[NSURLConnection alloc]initWithRequest:request delegate:self];

CFRunLoopRun();

So, assuming this is all being called on the main thread, will it block the main thread? Or will it spawn a new thread through the CFRunLoopRun() function call?

Thanks!


Solution

  • Assuming this is called from the main thread, there isn't really any reason to call CFRunLoopRun, because the default run loop should already be running.

    The way you're using NSURLConnection will not block the calling thread. It may spawn additional threads internally, but you don't really have to care about that. initWithRequest:delegate: will return pretty much immediately and your delegate methods will be called later (when a response is received, data is loaded, etc.).