Introduction To RxSwift

What is RxSwift?

RxSwift, in its essence, simplify developing asynchronous programs by allowing your code to react to a new data and process it in sequential, isolated manner.

A normal IOS app would have the following:

  • A tab bar controls.
  • Data Coming from a Cloud Network.
  • Some other buttons.

To access the above asynchronously we do have the following:

  • Closures
  • Delegate Pattern
  • Notification Center
  • Grand Central Dispatch

But these are not designed for asynchronous sync that’s why most of the time we write synchronous code to make it flow like asynchronous code. That is where RxSwift come handy it helps you to write asynchronous code in more concise way.

Observable:

The heart of RxSwift is based on observable also known as Sequence.

  • Observable are sequence can emit values. Values could be Interger, String, Dictionary, Arrays or anything else you like.
  • You can subscribe to the observable to get the updated value of the event e.g you’ve a slider control you can subscribe to it and whenever the slider moves you get the updated value.
  • Once the observable receives one of the following (Error, Completed) event it will stop producing any further elements.

Let’s have a look to a marble diagram for better understanding.

Implementing Observable:

Enough theory for the observable lets do some practical work. So I will assume that all of you already knows how to install pods in your xcode project. Go to your podfile and add the following:

  • pod ‘RxSwift’
  • pod ‘RxCocoa’
  • Observable.just will only accept one element that could be array, Int or String etc.
  • Observable2 will accept multiple elements but that doesn’t mean that its an array it will consider each element as a single element.
  • Observable3 is same as above observable2 only difference is we are passing an array but here the observable is considering the array as a single element. No matter how many elements you have inside the array each array is one element to the observer.
  • Just for better understanding observable4 is same like Observable3 & Observable2. So each array is single element to the observable4
  • Observable5 using .from we are passing an array and now each element of the array is considered as each element of the observable.

But what the user of an observable without a subscriber!!

On youtube people often subscribe to their favorite channel this allow them to get the notification of any new video uploaded to the subscribed channel. Similarly when we subscribe to the observer it triggers the observer to emit events until an error or completed events doesn’t occur.

RxSwift Subscribe

With the print statement above we can see the difference clearly between observable4 & observable5. Observable5 each element of array is considered single event where as in Observable4 each array is considered as single event.

But wait these are the events what about the values!! 🤯🤯🤯

So to get the values its fairly easy we have a method for subscribe i.e subscribe(onNext:) with this method you will get the values of events as you can see the arrow showing “Next” value on marble diagram.

Here is a quick implementation of subscribe(onNext:) .

When we subscribe onNext it provides us with options to handle Error, Completion & Disposing. But for this example I will just use onNext ignoring all other three.

Here we printed the values for observable5.

Have you noticed the second last image from here where we can see a weird option of onDisposed? what is it? why we never talked about it!!😮😧😱

Well lets talk about it now. When we subscribe to any observable the subscriber will keep on listening to the events and a memory will be allocated to it. But what when we are done with the subscriber how can we deallocate the resources we provided to subscriber to prevent memory leaks.

That’s where disposable comes in handy. When you call the disposable it will release all the resources allocated to the subscriber.

There are two ways to call it one manually other is using a disposebag first I will show you the manual way.

So we created a subscriber and after that we called the dispose function that’s a manual approach and its a bad practice. Let me show the other approach.

So the above image shows us the disposeBag approach which is pretty straight forward. Create the instance of DisposeBag and pass to the end of your subscriber. Now the subscriber will listen to the events once its done it will use the disposeBag to prevent memory leaks.

With all the above information provided I think by now you have a great understanding of RxSwift and how it works. I will be posting more articles about RxSwift practical implementation. Checkout my other article based on “What Are Subjects In RxSwift”.

--

--

--

An energetic and motivated individual IOS developer/ Data Science Practitioner. Apart from computer science Martial arts interests me the most.

Love podcasts or audiobooks? Learn on the go with our new app.

Building a Calendar Heatmap with SVG and React.js

20 JavaScript Shorthand Strategies that will spare your time

Dependencies you must know as a vuejs developer

Build modern chrome extension with React.js

Project Planning for a MERN Stack Project

How to Conditionally Invoke a Method

Angular Bootstrap with Dark Mode Toggle

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Umair Ishrat Khan

Umair Ishrat Khan

An energetic and motivated individual IOS developer/ Data Science Practitioner. Apart from computer science Martial arts interests me the most.

More from Medium

Easy Guide to RxDataSources!

Singly Linked Lists in Swift

How to create a new Xcode project without Storyboard ?

Dynamic table view footer with animations.