Publishers.Delay

μ œλ„€λ¦­ ꡬ쑰체 | μ•„λž˜μ— 흐λ₯΄λŠ” λ¦¬μ‹œλ²„μ—κ²Œ μš”μ†Œμ™€ μ™„λ£Œμ˜ 전달을 μ§€μ—°μ‹œν‚€λŠ” Publisher

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

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

  • interval : μ§€μ—°μ‹œν‚¬ μ‹œκ°„

  • tolerance : μ΄‰λ°œν•˜λŠ” μ§€μ—°λœ μ΄λ²€νŠΈμ—μ„œ ν—ˆμš©λœ λ‚΄μ„±

    • 이 ν”„λ‘œνΌν‹°λ₯Ό μ„€μ •λœ μ‹œκ°„λ§ŒνΌ μ‹œμŠ€ν…œμ€ 이벀트의 λ°°μΆœμ„ μ—¬μœ λ‘­κ²Œ ν•  수 μžˆλ‹€. μœ μ—°ν•˜κ²Œ(값이 크게) μ„€μ •λ μˆ˜λ‘ μ‹œμŠ€ν…œμ˜ μ „λ ₯ μ†ŒλΉ„λ₯Ό 쀄이고 응닡성을 μ’‹κ²Œ ν•  수 μžˆλ‹€.

  • scheduler : μ§€μ—°λœ 이벀트λ₯Ό μ „λ‹¬ν•˜λŠ” μŠ€μΌ€μ€„λŸ¬

  • options : μŠ€μΌ€μ€„λŸ¬ μ˜΅μ…˜

주어진 μ‹œκ°„λ§ŒνΌ μŠ€νŠΈλ¦ΌμœΌλ‘œλΆ€ν„°μ˜ κ°’ λ°°μΆœμ„ 미룬닀.

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

// Publishers.Delay Publisher
Publishers.Delay(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3]), interval: .seconds(1), tolerance: .zero, scheduler: DispatchQueue.main)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Delay Error")
    case .finished:
      print("Combine Delay Finish")
    }
  }, receiveValue: { value in
    print("Combine Delay : \(value)")
  })
  .store(in: &cancellables)

// delay Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
  .delay(for: .seconds(1), scheduler: DispatchQueue.main)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Delay Error")
    case .finished:
      print("Combine Delay Finish")
    }
  }, receiveValue: { value in
    print("Combine Delay : \(value)")
  })
  .store(in: &cancellables)

// 좜λ ₯은 1초 후에 λ°œμƒν•œλ‹€.
// Combine Delay : 1
// Combine Delay : 2
// Combine Delay : 3
// Combine Delay Finish

μƒμœ„ PublisherλŠ” 1, 2, 3의 값을 μ°¨λ‘€λŒ€λ‘œ λ‚Έλ‹€.

1초 μ§€μ—°ν•˜μ—¬ μ•„λž˜λ‘œ λ³΄λ‚΄λ―€λ‘œ 1초 후에 μ΄λ²€νŠΈκ°€ λ°œμƒν•œλ‹€.

RxSwift

delay μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬ν˜„ν•  수 μžˆλ‹€.

Observable.from([1, 2, 3])
  .delay(.seconds(1), scheduler: MainScheduler.instance)
  .subscribe(onNext: { value in
    print("RxSwift Delay : \(value)")
  }, onError: { _ in
    print("RxSwift Delay Error")
  }, onCompleted: {
    print("RxSwift Delay Finish")
  })
  .disposed(by: disposeBag)

// 좜λ ₯은 1초 후에 λ°œμƒν•œλ‹€.
// RxSwift Delay : 1
// RxSwift Delay : 2
// RxSwift Delay : 3
// RxSwift Delay Finish

ReactiveSwift

delay μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬ν˜„ν•  수 μžˆλ‹€.

SignalProducer([1, 2, 3])
  .delay(1, on: QueueScheduler.main)
  .start { event in
    switch event {
    case let .value(value):
      print("ReactiveSwift Delay : \(value)")
    case .failed:
      print("ReactiveSwift Delay Error")
    case .completed:
      print("ReactiveSwift Delay Finish")
    default:
      break
    }
  }

// 좜λ ₯은 1초 후에 λ°œμƒν•œλ‹€.
// ReactiveSwift Delay : 1
// ReactiveSwift Delay : 2
// ReactiveSwift Delay : 3
// ReactiveSwift Delay Finish

μ°Έκ³ 

ReactiveX - Operators - Delay

Last updated