Publishers.Throttle
์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์ฃผ์ด์ง ์๊ฐ ๋ด์ ์์ Publisher๊ฐ ๋ฐํํ ๊ฐ์ฅ ์ต์ ๋๋ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฐํํ๋ Publisher
์ด๋์ ๋ผ์ด์ ๋ ๋ค ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
upstream
: ์์์ ํ๋ฅด๋ Publisherinterval
: ๊ฐ์ฅ ์ต์ ์ ์์๋ฅผ ์ฐพ๊ณ ๋ฐฐ์ถํ ์๊ฐ ๊ฐ๊ฒฉ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
์ฐธ๊ณ
Last updated
Was this helpful?