Publishers.Debounce

μ œλ„€λ¦­ ꡬ쑰체 | 이벀트 간에 νŠΉμ • μ‹œκ°„μ΄ μ§€λ‚œ 후에야 μš”μ†Œλ₯Ό λ°œν–‰ν•˜λŠ” Publisher

μ΄λ‹ˆμ…œλΌμ΄μ €λŠ” 두 개의 인자λ₯Ό λ°›λŠ”λ‹€.

  • upstream : μƒμœ„μ— 흐λ₯΄λŠ” Publisher

  • dueTime : 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

μ°Έκ³ 

ReactiveX - Operators - Debounce

Last updated