RxSwift

Leela Krishna
4 min readJun 13, 2019

--

introduction

What is Rx? One of the definitions is:

ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences.

The basic idea of Reactive programming is, it focuses on asynchronous data streams, observe changes in underlying data and react accordingly using functional and declarative apis. The main advantage of this approach is it reduces global state and programmers can focus more on the business logic than worrying about state and its side effects.

Top →: Observable, Bottom →: Subscriber, Mid: Operators(transforms, combines, filters, etc takes place here)

Asynchronous Programming: Multiple tasks run at the same time, so those tasks execute or run on different threads / queues. iOS offers you all kind of APIs (like Operation Queues, GCD, Delegate, KVO & KVC, Notification-Center, etc) that allow you to perform different pieces of work on different threads and perform them across the different cores of the device’s CPU. Writing code that truly runs in parallel, however, is rather complex, especially when different bits of code need to work with the same pieces of data. Rx solves this problem for you.

RxSwift :

RxSwift is a library for composing asynchronous & event based code by using

(The three building blocks of Rx code) to create reactive iOS Apps.

  • Observables
  • Operators
  • Schedulers

RxSwift addresses the following issues

  1. State
  2. imperative Programming
  3. Side Effects
  4. Declarative Code
  5. Reactive Systems (Response, Resilient, Elastic & Message Driven Communications)

→ Observables :

  • An Observable emits notifications of change. A structure which emits a certain value over time. A subscriber subscribe to the observable.
  • An Observer subscribes to an Observable and gets notified when that Observable has changed.

The Observable<T> class provides the foundation of Rx code: the ability to asynchronously produce a sequence of events that can “carry” an immutable snapshot of data T. In the simplest words, it allows classes to subscribe for values emitted by another class over time. The Observable<T> class allows one or more observers to react to any events in real time.

An Observable can emit (and observers can receive) only three types of events:

  • A next event: An event which “carries” the latest (or “next”) data value. This is the way observers “receive” values.
  • A completed event: This event terminates the event sequence with success. It means the Observable completed its life-cycle successfully and won’t emit any other events.
  • An error event: The Observable terminates with an error and will not emit other events.

There are 2 kinds of observable sequences :

  1. Finite observable sequences : At some point terminates either with success or with error case. (Ex: Network calls)
  2. Infinite observable sequences : Never terminate (Ex: Device Orientations, UIActions, etc)
Image 1: Observable Sequence completing normally. Image2: Observable Sequence terminating with an error

→ Operators :

In simple words, Operators nothing but methods which are highly decoupled and composed together in order to build the complex logic. you can apply Rx operators to the pieces of input emitted by an Observable to deterministically process inputs and outputs until the expression has been resolved to a final value, which you can then use to build logic(to change behaviour of the app).

→ Schedulers :

Like in Swift, In Rx also there are DispatchQueues, Operations and Operation Queues. Schedulers are wrappers around these concurrent principles, which are very easy and convenient to use.

RxSwift comes with a number of predefined schedulers, which cover 99% of use cases.

That being said, schedulers are very powerful.

For example, you can specify that you’d like to observe for next events on SerialDispatchQueueScheduler, which uses Grand Central Dispatch to serialize running your code on a given queue.

ConcurrentDispatchQueueScheduler will run your code concurrently. OperationQueueScheduler will allow you to schedule your subscriptions on a given NSOperationQueue.

Thanks to RxSwift, you can schedule the different pieces of work of the same subscription on different schedulers to achieve the best performance.

in the above image, Network call called on Custom NSOperation Scheduler, and then the response JSON mapped with in memory objects on Background Concurrent Scheduler, and then finally UI is updated on Main Thread Serial Scheduler.

→ My next articles on RxSwift :

— — — — — — — — — *********************** — — — — — — — — —

You can contact / follow me on twitter & linkedIn accounts.

Thanks for reading…

****************************!!!See you!!!****************************

--

--