[RxSwift] What are Filtering Operators?

봄인·2023년 3월 5일
0

About-RxSwift

목록 보기
3/5
post-thumbnail

There are several filtering operators. Let's find out about them.

Ignore Elements

Ignore elements filter simply allows to 'ignore' the elements.

let disposeBag = DisposeBag()
let strikes = PublishSubject<String>()

strikes
    .ignoreElements()
    .subscribe { _ in
        print("Subscription is called")
    }.disposed(by: disposeBag)

strikes.onNext("A")
strikes.onNext("B")
strikes.onNext("C")

If we run this code, nothing will happen because of the '.ignoreElement()' code. This code let publish subject to ignore the occured events(doesn't get to the code inside the closure).

strikes.onCompleted()

By adding this code above, at the end, the result will be the following:

Subscription is called

Subscription is only called when the subject is completed.


Element At Operator

'Element(At: Int)' returns the element at certain index.

strikes.element(at: 2)
    .subscribe(onNext: { _ in
        print("Index Reached!")
    }).disposed(by: disposeBag)

strikes.onNext("A")
strikes.onNext("B")
// If we run the code here, nothing will happen.

strikes.onNext("C")
// If we run the code here, 'Index Reached!' will be printed.

As we can see in the code above, except the certain index element, the elements will be ignored.


Filter Operator

Filter operator literally filters the elements according to the conditional expression.

Observable.of(1, 2, 3, 4, 5, 6, 7)
    .filter { $0 % 2 == 1 }
    .subscribe(onNext: {
        print($0)
    }).disposed(by: disposeBag)

If we run the code above, result will be the following:

1
3
5
7

This followed the conditional expression '$0 % 2 == 1' and only numbers made this expression true were printed when we called our subscription.


Skip Operator

Skip operator skips the values as many as the given count.

Observable.of("A", "B", "C", "D", "E", "F")
    .skip(2)
    .subscribe(onNext: {
        print($0)
    }).disposed(by: disposeBag)

The result of the code above is like this:

C
D
E
F

This code skipped 2 values in the front.

Skip While Operator

Skip while operator is another variation of 'Skip operator'. Skip while operator also requires conditional expression like filter operator does and it skips the elements according to the expression.

Observable.of(2, 2, 3, 2, 4, 5, 6)
    .skip(while: { $0 % 2 == 0 })
    .subscribe(onNext: {
        print($0)
    }).disposed(by: disposeBag)

If we run this code,

3
2
4
5
6

it will result like this.

Skip while operator skips 2(which makes the condition 'true') but it does not skip any values after when it meets 3(which makes the condition 'false'), even though the values make the condition 'true'.

= After meeting the value that makes condition 'false', all the values will be part of the observables.

Skip Until Operator

Skip until operator skips all the values until certain observable or subject is triggered.

let subject = PublishSubject<String>()
let trigger = PublishSubject<String>()

subject.skip(until: trigger)
    .subscribe(onNext: {
        print($0)
    }).disposed(by: disposeBag)

subject.onNext("A")
subject.onNext("B")

trigger.onNext("X")

subject.onNext("C")

The result of this code above will be like the following:

C

As we can see, only 'C' is printed because skip(until: ) operator skipped all the elements before the trigger.


Take Operator

Take operator takes the certain count of the values.

Observable.of(1, 2, 3, 4, 5, 6)
    .take(3)
    .subscribe(onNext: {
        print($0)
    }).disposed(by: disposeBag)

If we run the code above,

1
2
3

take operator took first three elements, so the first three elements will be printed.

Take While operator

Take while operator is a variation of take operator.
(It will be simple to consider as the opposite operator compared to 'skip while operator')

Observable.of(2, 4, 5, 8, 9, 10)
    .take(while: {
        return $0 % 2 == 0
    }).subscribe(onNext: {
        print($0)
    }).disposed(by: disposeBag)

The result will be following:

2
4

Take while operator only takes elements that make the conditional expression 'true' (in a row). When it meets certain value that makes the condition 'false', it will stop taking values(this is similar to skip while operator).

Take Until Operator

Another variation of the take operator will be the take until operator.

Take until operator takes all the elements until certain observable or subject is triggered.

let subject = PublishSubject<String>()
let trigger = PublishSubject<String>()

subject.take(until: trigger)
    .subscribe(onNext: {
        print($0)
    }).disposed(by: disposeBag)

subject.onNext("1")
subject.onNext("2")

trigger.onNext("X")

subject.onNext("3")

If we run this code,

1
2

we can see that take until operator took all the elements before the trigger, so 1 and 2 is printed.

profile
ᕙ(•̀‸•́‶)ᕗ

0개의 댓글