NSURL *httpsURL = [NSURL URLWithString:@"https://example.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:httpsURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0f];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
[self.connection start];
while(something) {
// some code
}
In the above code, the delegates (ex: willSendRequestForAuthenticationChallenge) are not executing until the while loop is completed. What is the reason for this behaviour and how to overcome this issue? I want the delegates to be executed in the background. How can I achieve it?
Thanks in advance.
NSURLConnection is deprecated. Use NSURLSession. When you do, set the session's
delegateQueueto a background queue. That way, your delegate messages can arrive. Right now, you are blocking them from arriving because they want to arrive on the main thread and they can't.However, as dgatwood as also said, if you are doing something time-consuming that can block the main thread, that is also just wrong. Your app will be killed by the WatchDog process if you try that sort of thing.