What is Reactive Programming | RxSwift + Bonus Books

Mammadowr
4 min readJul 23, 2022

--

If you did not yet read my previous article for every iOS Developers, then check it first, then come back here! :)
As I promised you, I am starting to explain all topics (one-by-one) that I mentioned before.

Today, with this article you will learn about reactive programming. Let’s see what you have in this article:

  1. Introduction to asynchronous programming.
  2. What is RxSwift?
  3. What is RxCocoa?
  4. Differences between Reactive Programming vs Imperative Programming
  5. Book advise
  • Introduction to Asynchronous Programming

When it comes to Asynchronous programming, the first thing that comes to mind is a solution that uses more than one thread. But it sounds to me like when you say “fruit”, most people immediately think about apple, however there is banana, grape, kiwi and etc. So from my POV, when you think of asynchronous programming, you need to first think that it gives the ability to continue other tasks that are not dependent on the result of this work without waiting for a long job to finish, and adds dependent tasks as a continuation to the expected work.

Asynchronous programming is a type of parallel programming in which a unit of work run seperately from the main application thread and when the work is completed, it informs the main thread if the work is completed or not. There are a lot of benefits of using it, as an example improved application performance and enhanced responsiveness. We told that it is good, and it has benefits, but as you know in programming, you should not use a good thing in everywhere. There will be some points that you would need to avoid it.

To make it easy to understand, let me give you couple of examples from an iOS app which you have worked at least one time in your life (of course if you are already coding. If not, then you saw and used it in your daily life when you use an app:

  • Playing audio or video
  • Pressing any buttons
  • Downloading bits of data from remote source
  • Animating the UIView transitions

It seems like everything is happening at the same time. Each time views are animated, the audio in your app doesn’t pause until the animation ends. Not all different parts of your program interfere with each other’s execution. There are various types of APIs that allow different threads to perform different threads in different execution contexts and perform them on different cores of the device’s CPU. Writing truly parallel code is complicated, especially when different pieces of code have to work with the same pieces of data. It is difficult to know which piece of code updates the data first or which code reads the last value.

  • What is RxSwift?

RxSwift is a library for composing asynchronous and event-based code by using observable sequences and functional style operators, allowing for parameterized execution via schedulers.
Source

Understanding the concepts behind reactive programming may seem daunting at first, but once you become familiar with reactive programming and RxSwift, it will help you write better applications. Especially when writing asynchronous and event-based code, RxSwift will help you maintain the code base with minimal effort.

  • What is RxCocoa?

Why do we need RxCocoa? Simply, because RxSwift is an implementation of the common, platform-independent Rx specification. As a result of that it does not recognise any Cocoa or UIKit specific class. In this case, RxCocoa will be our lifesaver because it will be helper library for RxSwift which contains all the classes that help develop specifically for UiKit and Cocoa. Instead of including some advanced classes, RXCocoa adds reactive extensions to many UI components so you can subscribe to various UI events right out of the box.

  • Differences between Reactive Programming vs Imperative Programming

Imperative Programming:
Traditionally, we write code that explains how it should solve a problem. Each line of code is executed sequentially to produce a desired result, which is known as imperative programming. The imperative paradigm forces programmers to write “how” a program will solve a given task. Notice the keyword “how” in the previous statement.

Reactive Programming:
Reactive programming is the practice of programming with asynchronous data streams or event streams. An event stream could be anything from keyboard inputs, button taps, gestures, GPS location updates, accelerometer, iBeacon, and so on. You can listen to a stream and react accordingly.

  • Books that you should read
  1. Florent Pillet, Scott Gardner and Marin Todorov:
    RxSwift - Reactive Programming with Swift
  2. Navdeep Singh:
    Reactive Programming with Swift 4
  3. Ash Furrow:
    Functional Reactive Programming on iOS

Basically that is all from my side. If you like my articles, if you think that they are useful, you can click on “Follow” button and share articles to reach more people. :)

--

--

Mammadowr

I speak 6 human and 5 programming languages. 🥳  iOS Developer. To support me: https://www.buymeacoffee.com/mammadowr8 ☕️