Apps nowadays deal with an abundance of real-time events that enable a highly interactive experience for the user. We need tools for properly dealing with these events. Is Apples latest framework an Answer for that?

Introduction

The Combine framework is the newest iOS declarative framework that helps with processing values over time.

Learning Combine Swift and SwiftUI is a must if you want to stay up to date with the world of iOS Development.

Combine raises the level of abstraction of your code so you can focus on the interdependence of events that define the business logic, rather than having to constantly fiddle with a large number of implementation details. The code will likely be more concise.

The Combine Framework is designed to give the following benefits to us:

Simple asynchronous code. Multithreading is simplified. Composable Components. Multi-Platform.

Getting in Reactive Mindset

Combine Swift is a Reactive Programming Framework. Reactive programming is basically programming with asynchronous data streams.

A stream can represent user interface events, network responses, scheduled events, and many other kinds of asynchronous data.

For Example with the button clicks, the stream would be something like the following.

Button Taps Over Time

We already have used asynchronous data streams in programming in iOS, Notification Centre or click events from UI elements are already asynchronous event streams. In a way, you observe events from them and decide how to respond to them.

With Combine Swift, we have the same idea, but we can create data streams out of anything. Everything should be thought of as a sequence of data over time.

What makes the Combine Swift even more powerful is that you have amazing functions provided from the framework. You can combine, merge, filter streams, use one stream as an input of another, buffer events until you want to react and much more.

Combine Swift Essentials

Combine Swift is one of the newest frameworks introduced by Apple. So firstly we will explore some of its essentials and later will get familiar using it.

1. Publishers and Subscribers

Most important things to understand in Combine are publishers and subscribers.

A publisher is a type that can deliver a sequence of values over time, and a Subscriber subscribes to the publisher to receive its elements.

Let’s create some simple publishers using Combine:

You can subscribe to a publisher by calling sink on in

OUTPUT:

Combine Swift

You can also use the sink method to receive an event when the publisher is finished

OUTPUT:

Combine Swift

finished

Subscribers can be also canceled at any time in order to avoid receiving events from the publishers by simply calling cancel on them

subscriber.cancel()

2. Subjects

Subjects in Combine are basically publishers on which we can subscribe but also dynamically send events to them.

We have two very useful Subject types on Combine Swift:

PassthroughSubject: If you subscribe to it you will get all the events that will happen after you subscribed.

CurrentValueSubject: Behaves similarly to the PassthroughSubject but also will give the new subscribers it's the most recent element.

Let’s see an example of a Subject in Combine:

A Subject can have also had multiple Subscribers. Let's demonstrate how it will behave when two different subscribers, subscribe to it at different times.

We can see that the subscribers will only receive elements that will be emitted after the subscription time.

3. Operators

What Makes The Combine Really powerful is the ability to transform, combine or filter the elements emitted by a publisher.

There are three groups of operators on Combine — Transforming, Filtering, and Combining Operators.

3.1 Transforming

Simplest Operator is Map Operator, it behaves similarly to Swift Map. It transforms values emitted from a publisher with a given function.

OUTPUT:

10

20

30

Flat Map is used to transform elements of a publisher to a sequence for a new or existing publisher.

3.2 Filtering

If you want to remove elements from a sequence of the publisher based on a specific condition, you should be using filter functions. In the example below, we will provide different possible filtering functions for our stream.

Filter removes elements that do not fit with our condition from closure function its analogous to the filter function of collections in Swift.

Remove Duplicates publishes only elements that don’t match the previous element.

Drop First omits the specified number of elements before republishing subsequent elements.

3.3 Combining

Combining operators help to combine elements from asynchronous sequences. Some operators are very similar to Swift operators when working with the collections. Let’s see some examples of them.

3.3.1 Merge

Combines elements from two different publishers of the same type, delivering a sequence of elements over time.

OUTPUT:

Munich is a city in europe

Berlin is a city in europe

Milano is a city in europe

Rome is a city in europe

3.3.2 Combine Latest

Combine Latest creates a publisher that receives and combines the latest elements from two publishers.

Let’s say we want to combine the value of a text field and filtering filed in order to use them on a network request. We can do something similar like the following:

3.3.3 Scan

Scan works similar to reduce in Swift. It combines with closure the current element with the last value returned by applying the closure function.

Output:

1

3

6

10

15

4. Schedulers

A Scheduler basically defines when and how to execute a closure. It can be really useful when we, for example, want to delay the execution of a specific function.

Let’s reuse our previous example of combining a selected filter with search text. This time we want to avoid when user types to fast and only get the events when there was 0.5 sec time in between. We can use debounce operator that does just that.

Solving Practical Problems

Combine Swift simplifies a lot of development tasks. In this section, we will provide a few helpful examples to understand the benefits of Combine Swift.

Notification Centre Publishers

Dealing with Notification-Centre observers can become declarative using Combine swift. By using Combine we can combine and transform notifications based on our needs.

We can get notifications in the form of publishers using Combine.

Also combining system notifications can become really helpful in some cases. The Following Example Merges Two Keyboard Notifications and maps them into current keyboard height. After that, it uses the height to assign to scroll view content insets.

Chaining Asynchronous Calls

Something that can be really useful with combine is the ease of chaining multiple asynchronous calls. The Following example demonstrates that

Performing Multiple Asynchronous Calls

Usually, when we want to wait for two asynchronous operations to finish in order to perform a task, we have to use DispatchGroups and to do manual management. With Combine Swift, we can simply use zip operator explained form above.

Evaluating Solutions

Solving Problems using Combine Swift can sometimes be really helpful, but in general, I would suggest the approach not to be all or nothing. I prefer personally to use reactive frameworks only where it makes sense. If something is already straight forward I don’t like to overcomplicate it by wrapping it in complex publishers just for the sake of using Combine.

Currently, Combine is still in its beta version but will be released later on this year. There are also alternatives to Combine like RxSwift and ReactiveSwift that are similar to Combine but have been out there for a while.

In comparison to other Reactive Swift frameworks, Combine turns to be much faster by writing the same function to all of the three frameworks we can see how much better the Combine Performs.

Here is the execution time from all of the frameworks, and we can see how much faster was Combine comparing to similar frameworks.

Execution Time In Seconds

Heap Allocations in Kilobytes

One downside of the Combine is that it will support only iOS 13 and later so apps that still support older OS Versions may need to stay on RxSwift or Reactive Swift for a little more time.

In Conclusion

The Combine framework is the newest iOS reactive framework that helps with handling data streams.

In this article, I provided explanations for the essentials of Combine Swift. We explained using examples basics of CombineSwift.

Later we applied our learnings in some practical iOS examples and saw how Combine Swift can make our code more simple and declarative.

Finally, we evaluated the framework in terms of the performance, stability and version support.

If you enjoy this article make sure to clap to show your support. Follow me to view many more articles that can take your iOS Developer skills to the next level.

If you have any questions or comments feel free to leave a note here or email me at arlindaliu.dev@gmail.com.