No more .resume() — Use Mock Data as you please — Or Live data if you need

Extend URLSession and never forget .resume() again

Also Introducing a protocol that will allow you to switch between live and mock data

Kenny Dubroff
The Startup
Published in
4 min readJun 27, 2020

--

Downloading medium.com with no .resume()
Look ma, no .resume()

If you work with networking in Swift, chances are you’ve used URLSession. Chances are also that you’ve spent 1, 2 or a hundred minutes debugging a call that just wouldn’t fire! Then you sheepishly realized you forgot .resume() at the end of your dataTask…

Why do I even need .resume() ?

If you look carefully at the way we do a network call, we aren’t immediately doing anything with the code inside of our dataTask. The dataTask method returns a URLSessionDataTask object that you can use for Asynchronous operations.

URLSession.shared.dataTask returns something!
URLSession.shared.dataTask returns something!

Once you call one of the methods on dataTask, things begin to happen. Focusing on our “culprit” here, the resume method…

URLSessionDataTask inherits from URLSessionTask which has our resume method:

--

--

Kenny Dubroff
The Startup

I’m a Full Stack iOS Developer with a passion for writing clean, reusable code.