header image with title async/await

Networking in SwiftUI with Async/Await

How to build a networking client in Swift using Async/Await and integrating it easily with SwiftUI!

Gary Tokman
4 min readOct 7, 2021

--

Introduction

Remember two years ago when almost every new project relied on a library for networking else the pyramid of doom would take over? URLSession completion handlers were too clunky, so we used libraries like Alamofire to provide an easy-to-use API for creating requests. With the recent additions to URLSession, 3rd party libraries seem like overkill for practical use cases.

In the recent release of Xcode 13, we received more updates to URLSession. The biggest being support for async/await. What exactly is it?

At a high level, they are new keywords added to Swift to specify when code needs to be run asynchronously, and we need to wait without blocking the thread. Let's look at a simple example.

  1. Task enables us to run background tasks concurrently async
  2. Await frees up the thread until the data task returns

--

--