Core Concepts of Combine

Publisher, Subscriber, Operators and Subjects

Steven Curtis
9 min readNov 24, 2020

--

Photo by Paweł Czerwiński on Unsplash

No matter your coding background, you will need to get to grips with some of the core concepts of Combine, what it does and how to perform some programming tasks in the same.

This article is here to help you!

What is Combine?

Combine is about what to do with values over time, including how they can fail.

This means that Combine uses many of the functional reactive concepts that are used by RxSwift (if you are familiar with that), but is Apple’s implementation is baked-in to the iOS SDK meaning it has the advantages of Swift, being type-safe and usable with any asynchronous API.

In addition to functional programming primitives like map, filter, and reduce you can split and merge streams of data.

These steams are important as we can visualise events (keyboard taps, touches etc.) as such a stream that can be observed. The observer pattern watches an object over time, notifying changes — and applied over time this would produce a stream of objects.

So Combine helps developers implement code that functionally describes actions that should be taken when data is received in such a stream.

When applied to an API, Combine allows us to handle errors as they happen and more easily process data that relies on asynchronous API. Applied to a UI, we can think of the user actions as a stream, and even test particular states that a host App can be in.

A good partner for Combine is SwiftUI, yet there is no reason why you can’t use UIKit to use Combine, the same way as we could happily use RxSwift with UIKit. I’ve created an example with a network manager that you might like to take a look at.

Understood. Let me describe them:

The core concepts

--

--