Articles, podcasts and news about Swift development, by John Sundell.

Combine logo

Combine

Discover how Apple’s Combine framework can be used to model increasingly complex asynchronous operations as reactive pipelines that emit values over time.

Let’s start with the basics

As a reactive programming framework, Combine enables various kinds of operations to be modeled as publishers that, when connected, can form asynchronous pipelines that let us process values and events through a series of stand-alone operators.

For example, a pipeline might start with a publisher that performs a network call to a given URL, which is then connected to operators that decode and cache the response of that network request, and finally the outcome of that entire pipeline is then observed in order to handle both successful and failed outcomes.

On this Discover page, you’ll find articles and podcast episodes that explore different aspects of Combine and how it can be used to solve real-world problems. We’ll also explore things like memory management, and how to unit test Combine-powered code.

Combine + SwiftUI

It’s probably not a coincidence that Combine happened to be introduced at the exact same time as SwiftUI. While Combine can certainly also be used with both UIKit and AppKit, as well as within scripts and command line tools, its highly declarative design often makes it a perfect logic companion to SwiftUI-based views.

Here are a few ways to use Combine to drive and update a SwiftUI view’s state:

Networking

Since Combine is primarily geared toward handling asynchronous pipelines and operations, it’s very often a great match for networking code, which is something that typically involves a fair amount of steps that can be implemented using Combine’s various operators — especially since Foundation’s URLSession API now ships with Combine support out of the box.

Concurrency

Starting with Swift 5.5, the language itself now has a native, built-in concurrency system that lets us perform various kinds of asynchronous operations without requiring any additional framework or abstractions. The good news is that it’s often possible (and sometimes quite simple, even) to mix Combine-based code with this new concurrency system. Here are a few ways to do just that:

Built-in and custom convenience APIs

The following articles explore some of the key convenience APIs that Combine ships with, which in many cases can enable us to build highly custom publishing logic without having to actually implement any custom Publisher types from scratch. We’ll also take a look at how we can construct our own set of convenience APIs for specific use cases as well.

Next, a quick message from this week’s sponsor

Swift by Sundell is completely free for everyone, but if you’d like to support my work, then please take just a few minutes to check out the following sponsor, since doing so really helps me out financially.

Essential Developer

Essential Developer: Learn the most up-to-date techniques and strategies for testing new and legacy Swift code in this free practical course for developers who want to become complete senior iOS developers. This virtual event includes lectures, mentoring sessions, and step-by-step instructions. Click to learn more.

Building custom publishers and pipelines

Next, let’s dive deeper into some of Combine’s more advanced aspects — which enable us to take complete control over how values are published, transformed, and merged.

Testing and memory management

The following articles contain tips and tricks on how to ensure that we won’t cause any memory leaks while setting up our Combine pipelines, as well as how to unit test Combine-based code:

How is the community using Combine?

Ever since its introduction in 2019, Combine has been a very commonly discussed topic on the podcast. These episodes are all particularly focused on Combine, with some of my friends from around the community sharing their tips on how they’re adopting Combine within their projects: