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