Beginning Networking with URLSession
Sep 13 2022 · Swift 5.6, iOS 15, Xcode 13.4.1
Part 1: Introduction to URLSession & Concurrency
Leave a rating/review
Notes: 01. Introduction
Hello hello! Welcome to the Beginning Networking with URLSession course. I’m Felipe and I’ll be your instructor as we dive into the wonderful world of networking.
You see, I’ve been building my own apps and projects. And while they are pretty cool and fun to use, they all live in their own little bubble with no way to connect or interact with the internet at all.
I have so many ideas, as you probably do as well, on how to download data from the internet, upload data to some server or web service, and enhance the overall interactivity and quality of life.
In this course you’ll take on the internet and bring your apps to a whole new level! Networking is a deep topic with lots and lots of things to consider. There are a variety of network protocols and proxies with edge cases for each of them. In order to provide a seamless experience, you need to account for all of them.
Thankfully, Apple does this for us in their Network framework. This framework exposes a lot of the moving parts which handles network communication.
But what if you just want to download a cute cat picture from the internet? The last thing you want to do is spend hours of your time making your way through pages of technical documentation for a complicated framework. I mean, you’re just trying to get a cat picture after all.
Apple has come to the rescue with the URLSession; An API built on top of the networking framework that handles all of the low level details of communicating across networks while allowing you control of the experience.
Prior to URLSession we had to use NSURLConnection and while it got the job done, it was clunky to manage. Doing anything off the beaten path meant writing your threading code, and the API itself was a bit clunky and unwieldy.
It was like trying to write an email with a cat on your keyboard. You could do it, but there are likely going to be lots typos involved.
Developers responded to this by creating their own solutions. Back in the old days, most apps included libraries such as Alamofire and AFNetworking to take the sting out of NSURLConnection. Apple took notice, however.
As of iOS 7 there’s URLSession. What used to be a painful experience for making even simple requests became an incredibly easy and intuitive API.
URLSession is a high-level framework that sits on top of the Apple Networking stack. This stack itself is highly optimized and designed to work on the variety of network types. If you were to write your own networking library, you’d need to account for all of these networks as well as the security that comes with accessing them.
By using URLSession, you get all these optimizations for free. Better yet, you don’t need to include any unnecessary dependencies into your app to get these features.
A URLSession can be thought of as a tab in your browser. For instance, when you go to a web page, the browser will make a series of requests to fetch all the assets that contribute to that page. Of course, you’ll quickly find that URLSession allows you to more than fetch data. URLSession comes with a variety of tasks for different jobs.
Another great feature of URLSession is that you can customize each session based on how you need it to work. For instance, you can have one session to download high priority images while another session runs in the background to downloads archived images.
This course is broken down into two parts. In part one, you’ll learn about concurrency and some of the basics of working with URLSession. Part two covers topics such as downloading data, caching policies, grouping requests, and pausing, cancelling, and resuming downloads.
You’ll see how URLSession works with the modern concurrency features of Swift, all while building a SwiftUI application. Before you can really leverage URLSession, you need to have a good understanding of the Swift language. This course uses Swift 5.6 on Xcode 13.
If your knowledge is out of date or you just need a refreshed, definitely check out our Programming in Swift course.
You’ll also need to have a basic understanding of concurrency. Concurrency is a challenging topic, but it’s critical when working over the network. As an example, When you are downloading files over the web, you don’t want the entire user interface to be frozen. Instead, you want to download your data in the background.
This course does provides a simple refresher, but for a deep dive, check out our course on Concurrency in this learning path. This course is provided with your subscription so definitely take a look if you are new to concurrency. A big thank you goes to Brian Mokley, who was the instructor on the previous version of this course!
Okay time for the show to start. Let’s dive into the world of URLSession by first reviewing concurrency.