Publishers.MeasureInterval

μ œλ„€λ¦­ ꡬ쑰체 | μƒμœ„ Publisherλ‘œλΆ€ν„° 전달받은 μ΄λ²€νŠΈλ“€ μ‚¬μ΄μ˜ μ‹œκ°„ 간격을 μΈ‘μ •ν•˜κ³  λ°°μΆœν•˜λŠ” Publisher

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

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

  • scheduler : μš”μ†Œλ₯Ό μ „λ‹¬ν•˜λŠ” μŠ€μΌ€μ€„λŸ¬

μ΄λ²€νŠΈκ°€ μΌμ–΄λ‚˜λŠ” μ‹œκ°„μ˜ 간격을 μΈ‘μ •ν•˜μ—¬ λ°œν–‰ν•œλ‹€.

measureInterval μ˜€νΌλ ˆμ΄ν„°μ™€ 관련이 μžˆλ‹€.

let subject = PassthroughSubject<Void, Never>()

// Publishers.MeasureInterval Publisher
Publishers.MeasureInterval(upstream: subject, scheduler: DispatchQueue.main)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine MeasureInterval Error")
    case .finished:
      print("Combine MeasureInterval Finish")
    }
  }, receiveValue: { value in
    print("Combine MeasureInterval : \(value.timeInterval)")
  })
  .store(in: &cancellables)

// measureInterval Operator
subject
  .measureInterval(using: DispatchQueue.main)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine MeasureInterval Error")
    case .finished:
      print("Combine MeasureInterval Finish")
    }
  }, receiveValue: { value in
    print("Combine MeasureInterval : \(value.timeInterval)")
  })
  .store(in: &cancellables)

Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in
  subject.send(Void())
}

// Combine MeasureInterval : nanoseconds(100535457)
// Combine MeasureInterval : nanoseconds(101047592)
// Combine MeasureInterval : nanoseconds(99104295)
// ...

0.1μ΄ˆλ§ˆλ‹€ subject에 Void 값을 μ „λ‹¬ν•˜λ„λ‘ ν•˜μ˜€λ‹€.

이벀트 간에 0.1초 μ–Έμ €λ¦¬μ˜ μ‹œκ°„ 간격이 λ°œμƒν•˜μ—¬ μ‹œκ°„ 간격이 κ°’μœΌλ‘œ λ°œν–‰λœλ‹€.

RxSwift

ν•΄λ‹Ή λ™μž‘μ„ κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

ReactiveSwift

ν•΄λ‹Ή λ™μž‘μ„ κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

μ°Έκ³ 

ReactiveX - Operators - TimeInterval

Last updated