Discovering URLSession Async API

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this lesson, you’ll discover the URLSession async/await APIs and implement an image downloader with download progress.

URLSession Async/Await APIs

URLSession offers several APIs to retrieve and upload data from and to the internet. These methods cover different scenarios and also provide different levels of abstraction to be used in them. You can use each one based on your current application and/or the feature you’re trying to implement.

Retrieving data from the internet

You already encountered this first method in the previous lesson to retrieve data.

Uploading data to the internet

The following two methods are equivalent to the previous ones, just for the upload direction:

Downloading files from the internet

This third method is dedicated to file downloading and has peculiarities in the content management that makes it different from the previous ones.

Getting session updates for downloads

Last but not least, this family of APIs allows you to download data from a server as in the first case, though they provide a sort of update mechanism using an AsyncSequence:

Adding the Article Image

Enough with the theory. :] Now, it’s time to get your hands dirty and implement some fancy stuff to enrich Apple News with a shining article preview image.

Downloading the Image

It’s time to put the theory into practice and implement the image downloading.

Adding a Download Progress Bar

As you can see, when the app downloads the image, it just shows a loading spinner. Wouldn’t it be nice to let the user know the download’s status?

Fixing the Download Progress

The download progress is a great addition that let the user understand what’s happening. Using async/await, you do the download on a background task, so you don’t block the main thread, allowing the user interface to remain fluid.

See forum comments
Download course materials from Github
Previous: Introduction Next: Image Downloader Demo