이번 포스팅에선 Observable을 필터링 할 수 있는 연산자들에 대해 알아봅시다~!
let disposeBag = DisposeBag()
let fruits = ["🍏", "🍎", "🍋", "🍓", "🍇"]
Observable.from(fruits)
.ignoreElements()
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
Completed
let disposeBag = DisposeBag()
let fruits = ["🍏", "🍎", "🍋", "🍓", "🍇"]
Observable.from(fruits)
.element(at: 1)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(🍎)
Completed
let disposeBag = DisposeBag()
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Observable.from(numbers)
.filter { $0.isMultiple(of: 2) }
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(2)
next(4)
next(6)
next(8)
next(10)
completed
let disposeBag = DisposeBag()
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Observable.from(numbers)
.skip(3)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(4)
next(5)
next(6)
next(7)
next(8)
next(9)
next(10)
completed
let disposeBag = DisposeBag()
let subject = PublishSubject<Int>()
let trigger = PublishSubject<Int>()
subject.skip(until: trigger)
.subscribe { print($0) }
.disposed(by: disposeBag)
subject.onNext(1)
trigger.onNext(0)
subject.onNext(2)
===============================================
결과
next(2)
let disposeBag = DisposeBag()
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Observable.from(numbers)
.skip(while: { !$0.isMultiple(of: 2) })
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(2)
next(3)
next(4)
next(5)
next(6)
next(7)
next(8)
next(9)
next(10)
completed
let disposeBag = DisposeBag()
let o = Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
o.take(10)
.skip(.seconds(3), scheduler: MainScheduler.instance)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(2)
next(3)
next(4)
next(5)
next(6)
next(7)
next(8)
next(9)
completed
let disposeBag = DisposeBag()
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Observable.from(numbers)
.take(5)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(1)
next(2)
next(3)
next(4)
next(5)
completed
let disposeBag = DisposeBag()
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Observable.from(numbers)
.take(while: { !$0.isMultiple(of: 2) }, behavior: .inclusive)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(1)
next(2)
completed
let disposeBag = DisposeBag()
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Observable.from(numbers)
.take(while: { !$0.isMultiple(of: 2) }, behavior: .exclusive)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(1)
completed
let disposeBag = DisposeBag()
let subject = PublishSubject<Int>()
let trigger = PublishSubject<Int>()
subject.take(until: trigger)
.subscribe { print($0) }
.disposed(by: disposeBag)
subject.onNext(1)
subject.onNext(2)
trigger.onNext(0)
subject.onNext(3)
===============================================
결과
next(1)
next(2)
completed
let disposeBag = DisposeBag()
let subject = PublishSubject<Int>()
subject.takeLast(2)
.subscribe { print($0) }
.disposed(by: disposeBag)
(1...10).forEach { subject.onNext($0) }
subject.onNext(11)
subject.onCompleted()
enum MyError: Error {
case error
}
subject.onError(MyError.error)
===============================================
결과
next(10)
next(11)
completed
struct Person {
let name: String
let age: Int
}
let disposeBag = DisposeBag()
let persons = [
Person(name: "Sam", age: 12),
Person(name: "Paul", age: 12),
Person(name: "Tim", age: 56)
]
Observable.from(persons)
.distinctUntilChanged(at: \.age)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(Person(name: "Sam", age: 12))
next(Person(name: "Tim", age: 56))
completed
// 짧은 시간동안 반복적으로 방출되는 이벤트 제어
// next 이벤트가 전달된 다음 지정된 시간이 경과하기까지 다른 이벤트가 전달되지 않는다면 마지막에 방출한 이벤트를 구독자에게 전달함
// (검색 기능 구현시 사용 사용자가 키워드를 검색할 때마다 네트워크 요청을 하거나 데이터베이스를 검색하는 경우 문자가 입력될때마다 매번 검색은 효율적이지 않음 -> 사용자가 문자를 연속적으로 입력할 때 호출하는 것은 효율적이지 않음 지정된 시간동안 문자를 입력하지 않으면 그때 네트워크 요청)
let disposeBag = DisposeBag()
let buttonTap = Observable<String>.create { observer in
DispatchQueue.global().async {
for i in 1...10 {
observer.onNext("Tap \(i)")
Thread.sleep(forTimeInterval: 0.3)
}
Thread.sleep(forTimeInterval: 1)
for i in 11...20 {
observer.onNext("Tap \(i)")
Thread.sleep(forTimeInterval: 0.5)
}
observer.onCompleted()
}
return Disposables.create {
}
}
// 지정된 시간동안 새로운 이벤트가 방출되지 않으면 가장 마지막에 방출된 이벤트를 구독자에게 전달함
buttonTap
.debounce(.milliseconds(1000), scheduler: MainScheduler.instance)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(Tap 10)
next(Tap 20)
completed
// 지정된 주기마다 하나씩 구독자에게 next 이벤트 전달 (짧은 시간 발생하는 버튼 탭과 delegate 메시지 처리시 사용)
let disposeBag = DisposeBag()
let buttonTap = Observable<String>.create { observer in
DispatchQueue.global().async {
for i in 1...10 {
observer.onNext("Tap \(i)")
Thread.sleep(forTimeInterval: 0.3)
}
Thread.sleep(forTimeInterval: 1)
for i in 11...20 {
observer.onNext("Tap \(i)")
Thread.sleep(forTimeInterval: 0.5)
}
observer.onCompleted()
}
return Disposables.create {
}
}
buttonTap
.throttle(.milliseconds(1000), scheduler: MainScheduler.instance)
.subscribe { print($0) }
.disposed(by: disposeBag)
===============================================
결과
next(Tap 1)
next(Tap 4)
next(Tap 7)
next(Tap 10)
next(Tap 11)
next(Tap 12)
next(Tap 14)
next(Tap 16)
next(Tap 18)
next(Tap 20)
completed