✨
Combine 🀝 RxSwift 🀝 ReactiveSwift
  • Combine 🀝 RxSwift 🀝 ReactiveSwift
  • Publisher
    • Publishers.Merge
    • Publishers.Zip3
    • Publishers.TryScan
    • Publishers.MergeMany
    • Publishers.Breakpoint
    • AnyPublisher
    • Publishers.Concatenate
    • Publishers.Drop
    • Publishers.Decode
    • Publishers.TryCompactMap
    • Publishers.CombineLatest4
    • Optional.Publisher
    • Publishers.CollectByCount
    • Publishers.TryFilter
    • Publishers.CombineLatest3
    • Publishers.TryContainsWhere
    • Just
    • Publishers.TryFirstWhere
    • Publishers.MapError
    • Publishers.RemoveDuplicates
    • Result.Publisher
    • Publishers.LastWhere
    • Publishers.First
    • Publishers.Throttle
    • Publishers.DropUntilOutput
    • Publishers.Count
    • Publishers.Comparison
    • Publishers.Debounce
    • Publishers.MapKeyPath
    • Publishers.CollectByTime
    • Publishers.TryDropWhile
    • Empty
    • Publishers.PrefixUntilOutput
    • Publishers.Print
    • URLSession.DataTaskPublisher
    • Publishers.Reduce
    • Publishers.MapKeyPath3
    • Publishers.Collect
    • Publishers.TryPrefixWhile
    • Publishers.MapKeyPath2
    • Future
    • Publishers.SubscribeOn
    • Publishers.AllSatisfy
    • Publishers.Retry
    • Publishers.ContainsWhere
    • Publishers.SetFailureType
    • Publishers.TryMap
    • Publishers.Output
    • Publishers.TryRemoveDuplicates
    • Publishers.HandleEvents
    • Publishers.Sequence
    • Publishers.TryCatch
    • Publishers.Merge8
    • Publishers.CombineLatest
    • Deferred
    • Publishers.Merge7
    • Publishers.Contains
    • Fail
    • Publishers.Merge6
    • Publishers.TryComparison
    • Publishers.Delay
    • Publishers.Scan
    • Publishers.Merge5
    • Publishers.MeasureInterval
    • Publishers.AssertNoFailure
    • Publishers.ReceiveOn
    • Publishers.Merge4
    • Publishers.DropWhile
    • Publishers.Merge3
    • Publishers.TryReduce
    • Publishers.Zip
    • Publishers.Timeout
    • Publishers.CompactMap
    • Publishers.ReplaceEmpty
    • Publishers.PrefixWhile
    • Publishers.Filter
    • Publishers.TryAllSatisfy
    • Publishers.FirstWhere
    • Publishers.Last
    • Publishers.Map
    • Publishers.SwitchToLatest
    • Publishers.TryLastWhere
    • Publishers.Encode
    • Publishers.Catch
    • Publishers.ReplaceError
    • Pulishers.IgnoreOutput
    • Publishers.Zip4
  • Subject
    • PassthroughSubject
    • CurrentValueSubject
  • Subscriber
Powered by GitBook
On this page
  • RxSwift
  • ReactiveSwift
  • μ°Έκ³ 

Was this helpful?

  1. Publisher

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

μ°Έκ³ 

PreviousPublishers.FirstNextPublishers.DropUntilOutput

Last updated 5 years ago

Was this helpful?

ν˜„μž¬ λŸ°νƒ€μž„ μ—λŸ¬κ°€ λ°œμƒν•˜μ—¬ μ‹€μ œ λ™μž‘μ„ 확인할 수 μ—†λ‹€.

포럼
Publishers.Debounce