Publishers.Throttle

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์ฃผ์–ด์ง„ ์‹œ๊ฐ„ ๋‚ด์— ์ƒ์œ„ 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

Last updated

Was this helpful?