Combine - RxSwift's Successor

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.


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

The ‘Combine’ is Apple’s first asynchronous event-handling framework.
If you’ve done reactive programming earlier, you’d be already aware but
all you’ve done is only the imperative programming so far, then read this will serve
as a good starting point.


A generic definition of Reactive Programming goes like this:
Reactive programming is an asynchronous programming paradigm oriented around data streams and the propagation of change. In this case, reactive programming allows changes to be modeled as they propagate through a circuit.

These are the main components of the ‘Combine’ framework:
  • Publishers
  • Subscribers
  • Operators


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


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. 


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
RxSwift
Publisher
Observable
Subscriber
Observer
receive(on:)
observeOn


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.

Comments

Popular posts from this blog

Free Hand Drawing on Google's Map View

Android from iOS Guy's Perspective

Free Hand Drawing on Maps with MapBox SDK