Unlocking Reactive Programming: A Quick Overview

Category App Development

Reactive programming basically means relying on entities emitting elements, subscribing to its changes, and reacting to those changes, thereby saving you from having to re-check states and manually putting conditions in your code. It remarkably reduces the code lines and makes the code cleaner and easier to manage. Most importantly, it manages asynchronous code execution in a very elegant manner owing to its observable-subscriber method.

This blog article will give you a quick skim of elements of Reactive programming in under 4 minutes. Here are some basic terminologies that are used while writing code using the Reactive Programming paradigm.

Observable: It is an entity that can be of any data type and emits elements to its subscribers on the happening of every different type of event. Example: it emits error on an error event, emits the received value on an onNext event, etc.

Observer: The Observer subscribes to observables and listens for changes in them and takes the programmed action on any change.

Dispose Bag: A dispose bag holds disposables and is used for garbage management among the files. When subscriptions are created, they are added to a global dispose bag in a file. So whenever that particular files’ memory is deallocated, the subscriptions are properly disposed off from the Dispose bag thereby ensuring no memory leaks in the project.

Subjects: Subjects have a dual nature i.e., as an observable and as an observer. There are four types of subjects :

Publish Subject: Starts empty and only emits new elements to its subscribers. On completion, it emits completion to its new subscribers.

Behavior Subject: Starts with an initial value and replays it or the latest element to new subscribers.

Replay Subject: Initialized with buffer size and will maintain a buffer of elements up to that size and replay it to new subscribers.

Async Subject: Emits only the last next event in the sequence, and only when the subject receives a completed event.

Relays: Wraps their respective subjects, but only to accept and relay the next events. You cannot add a completed or error event onto relays at all, so they’re great for non-terminating sequences. There are two types of relays: PublishRelay and BehaviorRelay.

Operators: there are different types of operators such as filtering operators, transforming operators, time-based operators which can be chained on observables using the dot operator so that the input from the observable can be modified as per requirement.

Filtering operators: these operators filter out the results received from the data source. There are different types of filtering to do on data using different operators, for example, .elementAt(index): it gives the element at a particular index, .skip(n): it skips n elements emitted by data source, etc.

Transforming operators: these operators transform data received from the data source into a different type. Some examples of this are .toArray(): this operator is used to transform the received elements into an array, .map(): allows you to enter a data type and change it to another data type.

Combining operators: these operators combine results from observables. Example: .concat(): it is a static function that combines two observable sequences, .zip: it is used to couple two operations/requests and aggregate a result once both are done.

Time-based operators: these operators are used to do operations like buffering and replaying data. Example: replay(): This operator creates a new sequence which records the last ’n’ emitted by the source observable, interval(schedulers: ): it produces an infinite observable sequence of Int values (effectively a counter) sent at the selected interval on the specified scheduler.

Schedulers: A scheduler is a context on which a process takes place. So schedulers here are used to shift subscription contexts. There are different types of schedulers like MainScheduler, Concurrent Schedulers, etc. To Switch Schedulers, we use these two operators:

subscribeOn(): we can switch threads by using this operator.

observeOn(): this operator changes the scheduler where the observation happens.

About the Author

Smriti Arora is a young and passionate software development engineer and technical blogger with a knack for simplifying the technologies she works on for her readers. When she’s not writing or coding, she can found with loud music, setting the scene as she scours the internet in for tasty recipes and unique things to explore.

Ready to embark on a transformative journey? Connect with our experts and fuel your growth today!