Reactive Programming in Swift Part 1

Kishan Nakum
4 min readFeb 1, 2018

--

If you are an iOS Developer then you should have heard about RxSwift . It’s a swift version of Reactive programming . RxSwift is so powerful and effective that it will do all the work for you in code and make your life easy as developer .

Don’t Believe ? Than you might check this introduction of Reactive Programming to agree with me :) —-> Reactive Programming for newbies.

RxSwift is roaming around following terms:
- Observables & Observers
- Subject
- Operators
- Schedulers

let’s start exploring all these stuffs one by one.

Observables & Observers:

“ Event-A is emitting some data and other events (for example Event-B & Event-C) who subscribes to Event-A will get that emitted data. Event that emits the data (Event-A in this case) is called “Observable” or “Observable sequences” and all events (Event-B & Event-C in this case) that receives emitted data are called “Observers” .”

This is most simple definition to understand Observable.
Observable data can be continuous or emitting in more than one instance . Observers can receive all these instances one by one in sequence .

Let’s check the below diagram:

Here is how the coding representation in swift for above diagram:

let observableA = Observable.of(1,2,3,4,5,6)  // Creating Observablelet observerB = myFirstObservable.asObservable() // Subscribe to Observable
observerB.subscribe { event in
print(event)
}
Output:
next(1)
next(2)
next(3)
next(4)
next(5)
next(6)
completed

Here Observer B is getting continues emitted data from A. same funda will work for other Observers (C & D).

You might wonder about the terms “next” and “completed” in the above output. Let’s check this out.

Lifecycle of Observable:

Observables consist of three main events:

-Next
-Error
-Completed

two points to remember here:

(1) When observable emits the one element of data which is known as Next event.In the above case Observable A is emitting the data for six times. So observers will get that next events one by one.

(2) When observable will complete all the next event it will either terminate with completion or error.

--1--2--3--4--5--6--| // Completed--1--2--3--4--X // Error

Let’s check some examples:

// Observe Just 1 element
let observableOfJust = Observable.just("SWIFT")
observableOfJust.subscribe { event in
print(event)
}
Output:
next(SWIFT)
completed
...............................................................
// Observe an Array
let observablOfArray = Observable.of(["Ronaldo", "Messi", "Neymar"])
// You can also write next event like this
observablOfArray.subscribe(onNext: { (arr) in
print(arr)
})
Output:
["Ronaldo", "Messi", "Neymar"]
...............................................................
// Observe sequence of Data
let observableOfSequence = Observable.of("Ronaldo", "Messi", "Neymar")
observabl5.subscribe(onNext: { (str) in
print(str)
}, onError: { (error) in
print("I will execute if any error Occurs")
}, onCompleted: {
print("Completed Successfully")
})
Output:
Ronaldo
Messi
Neymar
Completed Successfully

If you are developing some serious apps than you should really care about memory leaks.So for that Observable has one important aspect which is known as “Dispose Bags”.

Once observables are done with all next events and terminated at the end , we need to release all the used resources to avoid memory leaks. For that we need to dispose on subscription.

// Continue with the above example
let disposeBag = DisposeBag()
let observableOfSequence = Observable.of("Ronaldo", "Messi", "Neymar")observabl5.subscribe(onNext: { (str) in
print(str)
}, onError: { (error) in
print("I will execute if any error Occurs")
}, onCompleted: {
print("Completed Successfully")
})
.disposed(by: disposeBag)

If you want to get more idea about how can you use this concept to make your code better , Here is some more conceptual things you can try:

In the next part we will see about Subjects and Operators in brief.

Feel free to add me on Linkedin for any questions.

Stay tuned…..

--

--