Skip to content

Instantly share code, notes, and snippets.

@pellet
Last active August 29, 2015 14:23
Show Gist options
  • Save pellet/5f901d47b69aafaa9e50 to your computer and use it in GitHub Desktop.
Save pellet/5f901d47b69aafaa9e50 to your computer and use it in GitHub Desktop.
NSURLProtocol with NSURLSession

My Rant

So I started writing my some swift code and decided to use a NSURLSession with my existing NSURLProtocol implementation instead of the as of now deprecated NSURLConnection. The protocol seemed to be working ok after adding it to the .customProtocols property on the NSURLSession.

However after attempting to run my tests, they were failing... why? I couldn't work out for hours...

I set up a break point in the canInit and startLoading methods. The application broke twice in the startLoading method, but only once in the canInit method... how could this be? Turns out that the NSURLProtocol was being 're-used' by NSURLSession, this mean't I now had to initialize the protocol object on every startLoad, not just when the object was actually initialised....

After making this fix, I noticed even after this the NSURLProtocol was constantly stopped and started mid way transfer, for this reason I decided to abandon using it via NSURLSession all together and explicitly now call startLoading and stopLoading with a request I set myself :)

@steshaw
Copy link

steshaw commented Jul 1, 2015

@pellet, I'm liking the markdown format 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment