RxSwift VS ReactiveSwift (Hot vs. Cold Signals)- Part 1

Mohammed Abdelaty
2 min readJun 23, 2020

--

learn the concept, not the library.

Photo by Temple Cerulean on Unsplash

I think it’s an important topic to discuss the difference between both Functional Reactive ways to implement in swift.

The most important requirements for recruiters and companies now are using RxSwift as a library itself not the understanding of reactive programming itself.

Understanding the concept is important and you decide which library would serve you better in your product.

1- Reactive Programming, which focuses on asynchronous data streams, which you can listen to and react accordingly.

2- Functional Programming, which emphasizes calculations via mathematical-style functions, immutability and expressiveness, and minimizes the use of variables and state.

These are the expressions which FRP comes from the combination of both.

RxSwift vs. ReactiveCocoa

It’s time to dig into the details. RxSwift and ReactiveCocoa handle several aspects of FRP differently, so let’s take a look at a few of them.

Hot vs. Cold Signals

let requestFlow = networkRequest.flatMap(parseResponse)

requestFlow.startWithNext {[weak self] result in
self?.showResult(result)
}
// Source : www.raywenderlich.com
  • A cold signal is a piece of work you start when you subscribe to it. Each new subscriber starts that work. Subscribing to the requestFlow three times means making three network requests.
  • A hot signal can already be sending events. New subscribers don’t start it. Normally UI interactions are hot signals.

ReactiveCocoa provides types for both hot and cold signals: Signal<T, E> and SignalProducer<T, E>, respectively. RxSwift, however, has a single type called Observable<T> which caters to both.

Independently of having different types or not, knowing about hot and cold signals is extremely important. As André Staltz puts it:

“If you ignore this, it will come back and bite you brutally. You have been warned.”

If you assume you are dealing with a hot signal and it turns out to be a cold one, you will be starting side effects for each new subscriber. This can have tremendous effects on your application. A common example would be three or four entities in your app wanting to observe a network request and for each new subscription, a different request would be started.

--

--