Introduction to Combine for Swift

Mid-2019 Apple introduced in its annual developer conference, its newest framework, Combine, a declarative framework with the premise of becoming the business logic orchestrator empowering the apps of tomorrow. Inspired by popular contemporary third-party libraries such as RXSwift and ReactSwift, Combine consists of a Publisher on one side, such as a RESTful API, and a Subscriber that processes data values over time, via data streams.

The programming paradigm is known as reactive programming, modeled around data flows propagating change and your UI controls reacting and adapting to those changes. The benefits of this framework inherently lies in allowing you To centralize event processing, making for a measurably high-performance architecture that is easier to read and maintain.

Apple had also coupled their announcement of Combine with SwiftIU, Apple’s move towards a declarative UI framework that is exceptionally simple in enabling you to build cross-device user interfaces that lets you focus on defining how you want your UI to look and work, without the pitfalls of imperative programming that make tracking transient states cumbersome and error-prone.

Declarative programming takes care of transitioning to states for you, once you declare all the possible states. This means that together with Combine, you don’t have to get bogged down in dealing with delegate callbacks or completion closures when handling and processing data events, and subsequently displaying them in your UI controls.

Publishers, Subscribers, and the Lifecycle

The foundations of Swift Combine consists of Publishers, Subscribers, and Operators.

Publishers and Subscribers 2.png

The quickest way I like to illustrate the framework is through a simple REST API, which acts as a publisher, streaming user analytics.

The counterpart to a publisher, is the subscriber, and you have numerous subscribers associated with that publisher. So we have a Subscriber and Publisher. Say the publisher asynchronously calls a URL and returns data. This bond creates a subscription that is given back to the subscriber.

Subsequently, the subscriber requests values from the publisher, ingesting a stream of events, getting each element event object. In most cases, you filter, manipulate, and reformat, before displaying it in your app UI, accomplished through an operator. When a publisher has no subscribers, it stops publishing.

So as you can see, unlike RxSwift, Combine is very much a Subscriber-driven framework. That is, if there are no publishers without subscribers, but rather subscribers dictate how much information to ingest, if any. This is done through what Apple terms, back pressure. You will learn all about it later in the course.

Suffice to say, the subscriber sets a specific Demand, whether there is a max number, a minimum number, unlimited, and so forth.

Power Up Your App with Combine

I’ve barely scratched the surface in this article on what Combine can do for you. In my latest LinkedIn learning course, Power up your app with Combine, I take you through a deep dive into the framework, stuffed with Xcode playground samples to help transition you into this modern framework.