[RxSwift] What are Observables?

봄인·2023년 3월 5일
0

About-RxSwift

목록 보기
1/5
post-thumbnail

Before following this post, installation of 'RxSwift' is necessary.
FOLLOW THIS

What is Observables?

In a dictionary sense, this word means 'that can be noticed or seen'
In RxSwift, observable is also known as 'sequence'. These can emit values. Those values can be integer, string, or other objects(dictionaries, arrays, etc).


Implementing Observables

To create an observable, we can simply call an observable class. Observable class contains a lot of different functions.

Observable.just

This will create an observable with 'just' one particular element.

let observableJ = Observable.just(1)

Observable.of

This will create an observable with arrays or set of different elements.

let observable1 = Observable.of(1, 2, 3)
let observable2 = Observable.of([1, 2, 3])

The difference between observable1(ob1) and observable2(ob2) is that ob1 will be functioning with single 'Int' element, and ob2 will be functioning with '[Int](an array of Int)' element.

Observable.from

This will create an observable with arrays but function on the individual elements of an array.

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

Subscribing Observables

(observable).subscribe

'Subscribe' is not going to give us the actual value of observables. This gives us the event.

observableF.subscribe { event in
	print(event)
}

The result of this code above is the following:

next(1)
next(2)
next(3)
next(4)
next(5)
completed

As we can see in the result, if the event is done, it prints 'completed'. But, how can we access to each elements? We have to unwrap the values like the following(using if let):

observableF.subscribe { event in
	if let element = event.element {
    	print(element)
    }
}

By accessing to events' elements, we can get the each values of elements. The result will be like following:

1
2
3
4
5
observableF.subscribe { event in
	if let element = event.element {
    	print(element)
    }
}

observable2.subscribe { event in
	if let element = event.element {
    	print(element)
    }
}

Now, we can find the difference between 'Observable.of' and 'Observable.from'. If run the same subscription code above, the result differs like following:

1
2
3
4
5
[1, 2, 3]

We can see the difference in the result. Observable.from handles each elements while Observable.of handles the whole array.

(obseravble).subscribe(onNext: )

Let's shorten the code of unwrapping each elements of observables. There is an parameter 'onNext' in the subscribe function. So we can use this parameter instead of using 'if let' to unwrap each elements.

observable4.subscribe(onNext: { element in
    print(element)
})

This code above will print out like following:

1
2
3
4
5

Disposing Observables

When we create an observable, it is important to surely dispose them. It is because when we create an subscription of an observable, the subscrber will always be listening or observing that particular sequence(observable).

So, we have to dispose these subscribers. If we do not dispose them, it can cause a memory leak.

let disposeBag = DisposeBag()

Observable.of("A", "B", "C")
    .subscribe{
        print($0)
    }.disposed(by: disposeBag)

We can simply dispose a subscriber by using the 'DisposeBag'. Using it like the code above.

profile
ᕙ(•̀‸•́‶)ᕗ

1개의 댓글

comment-user-thumbnail
2023년 4월 5일

who need help improving their coding skills bubble slides, daily game solve cross

답글 달기