1

NSURLSession is a refactoring of NSURLConnection and introduces some new features. I've been working on an issue with an app that uses both, and I'm wondering if this is OK. Or is it necessary to abandon NSURLConnection methods completely when adding NSURLSession?

Specifically, what's happening is that, seemingly randomly, a request is made and nothing is returned in response. If the webView is asked [webView isLoading], the answer is YES, however, neither connectionDidFinishLoading: nor URLSession:didBecomeIvalidWithError: nor webView:didFailLoadWithError: get called. Additionally, asking the web view to stop loading using [webView stopLoading] does not seem to work, since [webView isLoading] seems to continue to be YES.

If any of this sounds familiar, perhaps someone could give me suggestions of something to try. I'm running out of ideas. If NSURLConnection cannot safely be used while NSURLSession is being used, I'll just have to refactor to no longer use NSURLConnection, but I'd rather not if I don't have to or if doing so doesn't solve the problem.

Victor Engel
  • 2,037
  • 2
  • 25
  • 46

1 Answers1

0

Sounds to me like you are having threading issues. The difference with NSURLSession is the delegates fire on a background thread, so any direct UI updates from those methods won't work, you are expected to transfer back to the main thread first. Unless you are doing some heavy uploading or downloading you might not need that feature. To regain the original main thread design of NSURLConnection you can create a NSURLSession config with the main operation queue (means main thread):

 [NSURLSession sessionWithConfiguration:nil delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

Now all your delegates will be main thread. You can verify this with [NSThread isMainThread] then you are good to update the UI as you like.

malhal
  • 26,330
  • 7
  • 115
  • 133