Liam SY Kim
by Liam SY Kim
3 min read

Categories

RxSwift 02, Creating Operators (of, just, from, create..) with example code

Personally, I thought I understood RxSwift well, but it is still hard to adapt various operators on projects.

It is neccessary to arrange all of operators for using freely!

This posting is for beginer who want to know the concept of RxSwift operator.

What is Operator and when do we need to use?

Operator supports operation on stream. We usually operate Observables after subscription from observer.

However, Sometimes it is neccesary to calculate or create Observables before subscription.

Additionally, using operator is more intuitive and giving us clean code compare than only rely on calculating (actially it is impossible.. ) after subscription before subscription.

Operator gives you a lot of extensions to manage stream of RxSwift!

Kind of Operators

  • Creating Observables
  • Transforming Observables
  • Filtering Observables
  • Combining Observables
  • Error Handling Operators
  • Observable Utility Operators
  • Conditional and Boolean Operators
  • Mathematical and Aggregate Operators
  • Connectable Observable Operators

Today, we will study about Operators for creating observable!

Observable creation is the most basic action on RxSwift.

There are various operators to create observable.

You can use these depend on situations.

It is good to know well about those three operators of, just, from which are often used.

I will show more operator like range, repeatElement, timer, interval for creating observable which are not often used but for reference as also.

Create Operator

Using create() is the basic way to create Observabale.

For using create(), you should define all of events manually.

Altough it is hardest way to create Observable, Advantage of using create() is that you can define custom logic for Observable.

From observer -> Disposable, we can predict input of Observable is Observer.

When you subscribe() Observable, observer would be binded.

You can define events which should be emit to observer by adding onNext(), onError(), onComplete() behind observer.

let ob = Observable<Int>.create{ observer -> Disposable in
            observer.onNext(1)
            return Disposables.create()
        }

Execution

1

Of Operator

of() operator is simple form of emitting observable events in order.

let ob = Observable.of(1,2,3,4,5)

In this case, This observable emits 5 events.

Using Of operator do same action as below code.

let ob = Observable<Int>.create { observer -> Disposable in
            for i in 1...5 {
                observer.onNext(i)
            }
            observer.onCompleted()
            return Disposables.create()
        }

From above code, observable emits 5 events by using onNext().

This code is kind of often used form, RxSwift supports simple form of this logic by supporting of operator .

Execution

1
2
3
4
5

Just Operator

just() just emits a event in ()

let ob = Observable.just(1)

It is same logic of create() example which we saw at first.

Execution

1

From Operator

from() converts various objects like collection types into observables.

let ob = Observable.from([1,2,3,4,5])

You can see the input is array.

It emits element of array in order.

Execution

1
2
3
4
5

Then how does it action for other collection types?

This is the example of dictionary

let ob = Observable.from([1:1, 2:2, 3:3, 4:4, 5:5])

As you can see below, element of dictionary is emitting in order.

You can use from() operator for all collection types like these things.

Execution

(key: 2, value: 2)
(key: 4, value: 4)
(key: 3, value: 3)
(key: 1, value: 1)
(key: 5, value: 5)

Range Operator

From now, you might predict the action..

range() creates an Observable that emits a range of sequential integers

let ob = Observable.range(start: 1, count: 5)

Execution

1
2
3
4
5

RepeatElement Operator

repeatElement() is hot observable, emitting same event repeatly to observer.

let ob = Observable.repeatElement(1)

Execution

1
1
1
1
...

Timer Operator

timer() gives defined delayed time.

you can also define the scheduler for streaming.

It is not hot observable and onComplete() after emitting a event.

let ob = Observable<Int>.timer(4, scheduler: MainScheduler.instance)

Execution

0

Interval Operator

interval() gives defined interval time between each events.

you can also define the scheduler for streaming.

let ob = Observable<Int>.interval(1, scheduler: MainScheduler.instance)

Execution

0
1
2
3
4
...

Conclusion

I have never used Timer, ReapetElement, Interval, Range.

However, I try to use and practice all of operator.

The reason is for reference which I should use for coding clean code on unusal situation.

If you are first time to study operator, you don’t need to understand all.

Hope this posting is helpful ☺️