Skip to content

Instantly share code, notes, and snippets.

View pellet's full-sized avatar
🤡
Yay computers

Ben Pettit pellet

🤡
Yay computers
View GitHub Profile
@pellet
pellet / NSURLProtocol with NSURLSession pains.md
Last active August 29, 2015 14:23
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