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

Jed Fox
  • 2,979
  • 5
  • 28
  • 38
  • Don't use NSURLConnection. It is deprecated. – matt Aug 04 '16 at 20:26
  • Unrelated, but don't call `start` because `connectionWithRequest:delegate:` starts the connection automatically. The only time you call `start` is when you initiate the connection with `startImmediately` set to `false`. But it's moot, as you should use `NSURLSession`. – Rob Aug 04 '16 at 21:04

1 Answers1

1

NSURLConnection is deprecated. Use NSURLSession. When you do, set the session's delegateQueue to 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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • It isn't sufficient to let the run loop run only one time. All of the callbacks for NSURLConnection happen on the same thread that the connection was started on, so if you're doing lots of work in that same thread, you're going to delay every aspect of the connection—not just establishing the connection, but also sending headers, handling the initial response (status code and headers), reading/accumulating the response data, and telling your app that the request finished. Move whatever processing you're doing into a separate thread so that you don't block the main thread at all. – dgatwood Aug 04 '16 at 20:13
  • @dgatwood Good point, totally revised my answer. – matt Aug 04 '16 at 20:28
  • I can use nsurlconnection though it is deprecated as this is for enterprise distribution. The major problem is that I should return to the calling function once i get the response(simply saying, I want to do a synchronous request while handling the aurhentication) because this function is called by the javascript code and the native function should return the response back. If this is not achievable with nsurlconnection, then I would like to go with NSUrlSession. Please suggest me a way to perform this task – Bhargav Teja Aug 05 '16 at 03:27
  • I shall do nothing of the sort. I answered the question you asked. If you asked the wrong question, ask a new question, and try to do a better job of it this time. – matt Aug 05 '16 at 04:02