Combine -101: Introduction to Basics & Terms

Burcu K. Kutluay
Dev Genius
Published in
4 min readJan 30, 2022

--

With that article, my purpose is to be familiar with and learn the basics with the most common terms that create the Combine framework. When we complete this in the first step, we will be able to learn the details of this framework more easily after that. Let's start with some definitions part! (maybe the most boring part… Well yes, do not lose your motivation and keep reading!)

To start with, Combine was announced as a new framework at WWDC2019 from Apple. Combine is Apple’s own framework which can handle/help almost every task that IOS developers work on it. Wow, great but what? I mean Combine is really dynamic and powerful because thanks to it it is easier to handle delegates, notifications, callbacks, closures etc.

The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers. Source is available here.

Sure, great. Why do we need to use Combine FW instead of sending/observing notifications? Here are the answers:

Combine has 3 core concepts: Publishers, operators, subscribers. I read many things related to Combine framework. For introduction I started with trying to understand terms and responsibilities of publishers, operators, subscribers.

description source: https://developer.apple.com/documentation/combine

As you can see on the chart, it is the simplest explanation of the concepts. Let's go deeper: what actually are they, what are their responsibilities?

PUBLISHERS

The publisher is a protocol that is responsible for the distribution of the data/value over time to registered parties (such as subscribers, one or more) whenever it is available according to demand/request.

To make this happen, the requirement is there should be a subscription registered for the publisher. It means that there should be the request/listener to have the data from the publisher protocol.

To define a publisher, there are 2 associated types required: Output and Failure.

AnyPublisher<Output, Failure>

Output is a generic Output type that refers to the emitted value from the publisher. “Publisher.Output” is your emitted type which refers to the values that were published.
Failure is a form of error that the publisher returns.
BONUS: If you use Never as failure type, it means that it will never fail.

According to the following example, it means that the publisher emits Int as Output and it never fails.

Publisher<Int, Never>

OPERATORS

Operators are wide-range methods that do something and also change the data you are working on it, with the emitted values or the events (completion) that publishers emit.

Operators have always input (upstream) and output (downstream). They are the basics of “event-processing chains”. The input type of the operator must match with the publisher’s type.

SUBSCRIBERS

Subscribers are protocols that receive input from publishers. Every subscription ends with a subscriber.

Combine provides sink and assign subscribers as operators on the Publisher type:

  • sink(receiveCompletion:receiveValue:)provides closure-based behavior on your code so you will be able yo receive both values and completions. To understand better lets’ check the signature of the function.
  • assign(to:on:) whenever subscriber receives the Output property it is written on a variable and stored via key-path. For example if UI configurations need that recently received variable, it is easy to access and setup UI.

WTF key-paths?
Keypaths in Swift are a way of storing a reference to a property, as opposed to referencing property’s value itself. It’s like working with the name of the property, and not its value.

To understand better lets’ check the signature of the function.

Don’t worry if you feel you are still not familiar with Combine. It is very normal but you should know that this is only for pre-beginning. It is about reading, trying to understand the terms and after all concepts then trying how to implement Combine. First, in first, let's check for an overview:

For detailed content visit https://developer.apple.com/documentation/combine/subscriber/
  • Publisher is a protocol which defines requirements and publish values and events.
  • Publisher can publish zero or more values but contains only one completion event (error or a success state).
  • With just you can create a publisher from a single value.
  • If publisher publishes a completion event, it is done and cannot publish anymore.
  • Subscriber is a protocol which defines requirements to be able to receive input from the publisher.
  • Sink is a method which attaches a subscriber with closures.
  • With sink you can create a subscriber.
  • Sink will continue receive values as many as publisher publishes -> called as unlimited demand.

For the future articles, my purpose is going deeper into Combine FW concepts and writing first publishers, subscribers and playing with subjects.

Thank you for reading!

--

--

IOS Developer / Architectural Design — App Management / Guitar Player