Combine — RxSwift’s Successor

Saru
Mac O’Clock
Published in
4 min readApr 10, 2020

--

Photo by Carl Heyerdahl on Unsplash

Introduction

When I got to watch this WWDC video where the Apple engineers introduced a brand new Combine framework that enables developers to write asynchronous programs effectively, I was literally taken away after watching it. I will tell you the shocking story along the way.

When I got to watch this WWDC video where the Apple engineers introduced a brand new Combine framework that enables developers to write asynchronous programs effectively, I was literally taken away after watching it. I will tell you the shocking story along the way.

Let’s start by putting Apple’s definition for the Combine framework:

“A unified, declarative API for processing values overtime”

Combine introduces the framework’s main characteristics as:

  • Generic
  • Type-Safe
  • Composition first

A generic definition of Reactive Programming goes like this:

A generic definition of Reactive Programming goes like this:

These are the main components of the ‘Combine’ framework:

These are the main components of the ‘Combine’ framework:

  • Publishers
  • Subscribers
  • Operators
Photo by Smart on Unsplash

Publishers

‘Publisher’ is the most important declarative API of the ‘Combine’ framework.

Publishers are value types in Swift.

Subscribers can subscribe to the publisher to receive values emitted by the publisher

Combine’s Publishers Protocol
Photo by Jungwoo Hong on Unsplash

Subscribers

Subscribers are the ones that receive the values emitted from/by the publishers. There just can be any numbers of subscribers aka they mutatable and hence referenceable, so they are mostly classes.

Combine’s Subscriber Protocol
Photo by Adi Goldstein on Unsplash

Operators

These are like middlemen that act partly like publishers and partly like subscribers and allows us to do some amazing things by providing a ton of default operators.

For Ex:

‘receive(on:)’ is an operator that can be used to pass the data from a publisher that returns the data on the background thread to the subscriber on the main thread.

Looking at this high-level design, I was stunned to see how Apple’s engineers had bluntly stolen all the core concepts of RxSwift(ReactiveCocoa as well) like Publishers, subscribers, combineLatest, zip, etc [and not even changing their names ;) ] and has created its own copy-cat version called ‘Combine’ by applying some of their own methodologies to fit into the Swift world.

Apple still seems to be following the core principles defined by the late and great Steve Jobs:

“Good artists copy; great artists steal.”

Combine Vs RxSwift

Here are major counterparts:

Combine Vs RxSwift

This cheat sheet is for those who’ve used RxSwift already.

Combine

Pros:

  • Works well with SwiftUI by modifying the model properties which lets the SwiftUI generate the views dynamically using @State, @Binding @BindableObject APIs
  • Built directly into the OS
  • Highly performant
  • Support Cross Apple Platforms

Cons:

  • Relatively new and hence would get minimal help with issues/documentation
  • Support only iOS13 and up devices
  • RxCocoa gives us powerful API’s to streams data from UI components and works well with UIKit, Combine doesn’t provide anything as such
  • Not community-driven aka closed source project

Conclusion

I suppose that RxSwift’s popularity has only gone up after Combine is released but it might eventually come to an end in the future since Combine is natively built into the OS and has way more power in comparison and is clearly a winner.

But, that’s not going to happen sooner because ‘Combine’ is still relatively new and the fact that it only supports iOS13 and up pales in comparison to RxSwift’s iOS8 and up and Apple’s don’t seem to have any backward compatibility plans for this.

If you’re still interested in learning RxSwift with the VIPER design pattern, then this course should give you a headstart which is still relevant to this date.

Originally published at https://saru2020.blogspot.com on April 10, 2020.

--

--

Saru
Mac O’Clock

Passionate Technologist 📱, Writer ✍️, Music Junkie 🎵, Movie Buff 🎬, Swimathoner 🏊, Sociologist 📢, Lover of Food🍺 & Life🌱!