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