Basics of Reactive Programming In Swift (RxSwift)

Category App Development, Product engineering

Reactive programming basically means relying on entities emitting elements, subscribing to its changes and reacting to those changes. So, reactive programming saves 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 article will give you a quick skim of elements of Reactive programming in just 4 min reading time. These are some basic terminologies that are used while writing code using the Reactive Programming paradigm.

  1. Observable: it is an entity that can be of any data type and emits elements to its subscribers on happening of every different type of event. Example: it emits error on an error event, emits the received value on an onNext event, etc.
  2. Observer: This subscribes to observables and listens for changes in them and takes the programmed action on any change.
  3. Dispose Bag: A dispose bag holds disposables in it. So basically, it is used for garbage management in the files. When we create subscriptions, we keep adding them 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, so this ensures there are no memory leaks in the project.
  4. Subjects: Subjects acts dual way, both as an observable as well as an observer. There are four types of subjects :
  • Publish Subject: it starts empty and only emits new elements to its subscribers. On completion, it still 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.

5. Relays: These wrap their respective subjects, but only 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 sequencesThere are two types of relays: PublishRelay and BehaviorRelay.

6. 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 let you filter out the results received from the data source. There are different type of filtering you can 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 let you 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 let you 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.

7. 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.

That’s all about the basics of Reactive Programming and a sneak peek into Rxswift.

I hope it was helpful enough to give you a kick start on reactive programming and rxswift!

Please share your valuable feedback in the comments section and also give the article a Clap if it’s worth it!

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