RxSwift 의 Operator 들을 사용방법들을 간단하게 살펴보자
map
import RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3)
.map { $0 * 2 }
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// 출력결과
2
4
6
filter
import RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5)
.filter { $0 % 2 == 0 }
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// 출력결과
2
4
flatMap
import RxSwift
let disposeBag = DisposeBag()
let observable1 = Observable.of(1, 2)
let observable2 = Observable.of(3, 4)
observable1.flatMap { _ in
return observable2
}.subscribe(onNext: {
print($0)
}).disposed(by: disposeBag)
/// 출력 결과
3
4
3
4
concat
import RxSwift
let disposeBag = DisposeBag()
let observable1 = Observable.of(1, 2, 3)
let observable2 = Observable.of(4, 5, 6)
Observable.concat([observable1, observable2])
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// 출력결과
1
2
3
4
5
6
merge
import RxSwift
let disposeBag = DisposeBag()
let subject1 = PublishSubject<Int>()
let subject2 = PublishSubject<Int>()
Observable.of(subject1, subject2)
.merge()
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
subject1.onNext(1)
subject2.onNext(2)
subject1.onNext(3)
subject2.onNext(4)
/// 출력결과
1
2
3
4
combineLatest
import RxSwift
let disposeBag = DisposeBag()
let observable1 = BehaviorSubject(value: "Hello")
let observable2 = BehaviorSubject(value: "World")
Observable.combineLatest(observable1, observable2) { text1, text2 in
return "\(text1) \(text2)"
}.subscribe(onNext: {
print($0)
}).disposed(by: disposeBag)
observable1.onNext("Hi")
observable2.onNext("RxSwift")
/// 출력결과
Hello World
Hi World
Hi RxSwift
zip
import RxSwift
let disposeBag = DisposeBag()
let observable1 = Observable.of(1, 2, 3)
let observable2 = Observable.of("A", "B", "C")
Observable.zip(observable1, observable2) { number, letter in
return "\(number) \(letter)"
}.subscribe(onNext: {
print($0)
}).disposed(by: disposeBag)
/// 출력결과
1 A
2 B
3 C
scan
import RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5)
.scan(0) { accumulator, value in
return accumulator + value
}
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// 출력 결과
1
3
6
10
15
reduce
import RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5)
.reduce(0) { accumulator, value in
return accumulator + value
}
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// 출력 결과
15
distinctUntilChanged
import RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 2, 3, 3, 3, 4, 4, 5)
.distinctUntilChanged()
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// 출력 결과
1
2
3
4
5
take
import RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5)
.take(3)
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// 출력 결과
1
2
3
skip
import RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5)
.skip(2)
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// 출력 결과
3
4
5
takeUntil
import RxSwift
let disposeBag = DisposeBag()
let stopObservable = PublishSubject<Void>()
Observable.of(1, 2, 3, 4, 5)
.takeUntil(stopObservable)
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
// 두 번째 Observable이 방출되면서 첫 번째 Observable의 방출이 중단됩니다.
stopObservable.onNext(())
/// 출력 결과
/// stopObservable.onNext(())가 호출되는 시점에 따라 출력 결과가 달라질 수 있음.
1 or 1 2 3
withLatestFrom
import RxSwift
let disposeBag = DisposeBag()
let buttonTap = PublishSubject<Void>()
let textFieldInput = PublishSubject<String>()
buttonTap
.withLatestFrom(textFieldInput)
.subscribe(onNext: {
print("Button tapped with text: \($0)")
})
.disposed(by: disposeBag)
textFieldInput.onNext("Hello")
buttonTap.onNext(())
textFieldInput.onNext("World")
buttonTap.onNext(())
/// 출력 결과
Button tapped with text: Hello
Button tapped with text: World
debounce
import RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5)
.debounce(.milliseconds(500), scheduler: MainScheduler.instance)
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
// 방출 시간 간격이 짧아서 모든 항목이 출력되지 않을 수 있습니다.
throttle
import RxSwift
import RxCocoa
let disposeBag = DisposeBag()
// 버튼 클릭 이벤트를 시뮬레이션하기 위한 PublishSubject 생성
let buttonTap = PublishSubject<Void>()
// 1초 동안 한 번만 이벤트를 방출하도록 throttle 적용
buttonTap
.throttle(.seconds(1), scheduler: MainScheduler.instance)
.subscribe(onNext: {
print("Button tapped")
})
.disposed(by: disposeBag)
// 버튼 클릭 시뮬레이션
buttonTap.onNext(())
buttonTap.onNext(())
buttonTap.onNext(())
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
buttonTap.onNext(())
}
/// 출력 결과
Button tapped
Button tapped