μ λ€λ¦ ꡬ쑰체 | μ£Όμ΄μ§ μκ° λ΄μ μμ Publisherκ° λ°νν κ°μ₯ μ΅μ λλ 첫 λ²μ§Έ μμλ₯Ό λ°ννλ Publisher
μ΄λμ
λΌμ΄μ λ λ€ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher
interval
: κ°μ₯ μ΅μ μ μμλ₯Ό μ°Ύκ³ λ°°μΆν μκ° κ°κ²©
scheduler
: μμλ₯Ό λ°ννλ μ€μΌμ€λ¬
latest
: κ°μ₯ μ΅μ μ μμλ₯Ό λ°ννλμ§λ₯Ό κ°λ¦¬ν€λ λΆλ¦¬μΈ κ°
μμκ° λ°°μΆλκ³ μ£Όμ΄μ§ μκ°μ΄ μ§λκ³ μμΌ κ·Έ μκ° λ΄μ λ°°μΆλ 첫 λ²μ§Έ λλ κ°μ₯ μ΅μ μ μμλ₯Ό λ°ννλ€.
debounce
λ μ£Όμ΄μ§ μκ° λμ κ°μ μ λ¬νμ§ μμμΌ λ§μ§λ§μ μ λ¬λ κ°μ λ°ννμ§λ§, throttle
μ μ£Όμ΄μ§ μκ°μ΄ μ§λ λ κ·Έ μκ° λ΄μ λ°°μΆλ 첫 λ²μ§Έ λλ κ°μ₯ μ΅μ μ μμλ₯Ό λ°ννλ€.
throttle
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
let subject = PassthroughSubject<Int, Never>()
// Publishers.Throttle Publisher
Publishers.Throttle(upstream: subject, interval: .seconds(1), scheduler: DispatchQueue.main, latest: true)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Throttle Error")
case .finished:
print("Combine Throttle Finish")
}
}, receiveValue: { value in
print("Combine Throttle : \(value)")
})
.store(in: &cancellables)
// Combine Throttle : 9
// throttle Operator (latest: true)
subject
.throttle(for: .seconds(1), scheduler: DispatchQueue.main, latest: true)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Throttle Error")
case .finished:
print("Combine Throttle Finish")
}
}, receiveValue: { value in
print("Combine Throttle : \(value)")
})
.store(in: &cancellables)
// Combine Throttle : 9
// throttle Operator (latest: false)
subject
.throttle(for: .seconds(1), scheduler: DispatchQueue.main, latest: false)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Throttle Error")
case .finished:
print("Combine Throttle Finish")
}
}, receiveValue: { value in
print("Combine Throttle : \(value)")
})
.store(in: &cancellables)
// Combine Throttle : 0
for i in 0 ..< 10 {
subject.send(i)
}
1κ³Ό 2μ μ½λλ κ°μ΄ λ°νλλ©΄ 1μ΄μ μΉ΄μ΄νΈλ₯Ό μ μ΄ μΉ΄μ΄νΈκ° λλλ©΄ ν΄λΉ μκ° λ΄μ λ°νλ μ΅μ μ μμλ₯Ό λ°ννλ€.
3μ μ½λλ κ°μ΄ λ°νλλ©΄ 1μ΄μ μΉ΄μ΄νΈλ₯Ό μ μ΄ μΉ΄μ΄νΈκ° λλλ©΄ ν΄λΉ μκ° λ΄μ λ°νλ 첫 λ²μ§Έ μμλ₯Ό λ°ννλ€.
μ½λλ₯Ό μ€ννλ©΄ λ°λ³΅λ¬Έμ ν΅ν΄ 0λΆν° 9κΉμ§ subject
μ κ°μ μ λ¬νλλ°, μ΄ λμμ΄ 1μ΄ μμ λͺ¨λ μ΄λ£¨μ΄μ§λ―λ‘ μΉ΄μ΄νΈκ° λλ λ μ€νΈλ¦Όμ 첫 λ²μ§Έ κ°μ 0, μ΅μ κ°μ 9μΌ κ²μ΄λ€.
νμ¬ λ°νμ μλ¬κ° λ°μνμ¬ μ€μ λμμ νμΈν μ μλ€. ν¬λΌ
RxSwift
throttle
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
μΉ΄μ΄νΈμ μμμ λΆλ¬ μΌμΌν€λ 첫 λ²μ§Έ κ°κ³Ό μΉ΄μ΄νΈκ° λλ λμ μ΅μ κ°μ λͺ¨λ λ°°μΆνλ κ²μΌλ‘ ꡬνλμ΄ μλ€.
let subject = PublishSubject<Int>()
subject
.throttle(.seconds(1), scheduler: MainScheduler.instance)
.subscribe(onNext: { value in
print("RxSwift Throttle : \(value)")
}, onError: { _ in
print("RxSwift Throttle Error")
}, onCompleted: {
print("RxSwift Throttle Finish")
})
.disposed(by: disposeBag)
for i in 0 ..< 10 {
subject.onNext(i)
}
// RxSwift Throttle : 0
// RxSwift Throttle : 9
ReactiveSwift
throttle
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
let property = MutableProperty<Int>(0)
property.signal
.throttle(1, on: QueueScheduler.main)
.observe { event in
switch event {
case let .value(value):
print("ReactiveSwift Throttle : \(value)")
case .failed:
print("ReactiveSwift Throttle Error")
case .completed:
print("ReactiveSwift Throttle Finish")
default:
break
}
}
for i in 0 ..< 10 {
property.value = i
}
// ReactiveSwift Throttle : 9
μ°Έκ³
Publishers.Debounce