본문 바로가기
iOS/RxSwift

distinctUntilChanged(compare: )

by 패쓰킴 2023. 2. 20.
728x90
let numbers = [1, 1, 2, 4, 4, 3, 5, 7, 1]
Observable.from(numbers)
    .distinctUntilChanged { !$0.isMultiple(of: 2) && !$1.isMultiple(of: 2) }
    .subscribe { print($0) }
    .disposed(by: disposeBag)

 

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

 

과정

1       -> 1
1 1 true true    
1 2 true false -> 2
2 4 false false -> 4
4 4 false false -> 4
4 3 false true -> 3
3 5 true true    
3 7 true true    
3 1 true true    

1. 첫번째 값은 무조건 방출한다

2. 방출된 값과 오른쪽 값을 비교한다

    홀수는 true

    짝수는 false

3. 비교하여 false값이 있다면 비교 값의 오른쪽 값을 방출한다.

4. 2번과 3번 반복

728x90

'iOS > RxSwift' 카테고리의 다른 글

Action  (0) 2024.01.02
Button  (0) 2023.12.21
기본  (0) 2023.11.23
TableView  (0) 2023.03.02

댓글