Publishers.Retry

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์‹คํŒจํ•œ ์ƒ์œ„ Publisher์— ๋Œ€ํ•˜์—ฌ ๊ตฌ๋…์„ ๋‹ค์‹œ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ์‹œ๋„ํ•˜๋Š” Publisher

์ด๋‹ˆ์…œ๋ผ์ด์ €๋Š” ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋‹ค.

  • upstream : ์ƒ์œ„์— ํ๋ฅด๋Š” Publisher

  • retries : ์ตœ๋Œ€ ์žฌ์‹œ๋„ ํšŸ์ˆ˜. nil์ด ๋“ค์–ด๊ฐ€๋ฉด ์ƒ์œ„ Publisher๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋‚ด์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ์žฌ์‹œ๋„ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ํŠน์ • ๋„คํŠธ์›Œํฌ ์š”์ฒญ์ด ์„ฑ๊ณตํ•  ๋•Œ๊นŒ์ง€ ๋‹ค์‹œ ์š”์ฒญํ•˜๋ ค ํ•œ๋‹ค๋ฉด ์ด ๋™์ž‘์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

retry ์˜คํผ๋ ˆ์ดํ„ฐ์™€ ๊ด€๋ จ์ด ์žˆ๋‹ค.

// Publishers.Retry Publisher
Publishers.Retry(upstream: Fail<Void, Error>(error: error), retries: 2)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Retry Error")
    case .finished:
      print("Combine Retry Finish")
    }
  }, receiveValue: { value in
    print("Combine Retry : \(value)")
  })
  .store(in: &cancellables)

// retry Operator
Fail<Void, Error>(error: error)
  .retry(2)
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Retry Error")
    case .finished:
      print("Combine Retry Finish")
    }
  }, receiveValue: { value in
    print("Combine Retry : \(value)")
  })
  .store(in: &cancellables)

// Combine Retry Error

์œ„์˜ ์ฝ”๋“œ๋Š” ์ƒ์œ„ Publisher๋กœ Fail Publisher๋ฅผ ๋ช…์‹œํ•˜์˜€๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ฒซ ๊ตฌ๋…์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚ด๋ ค์˜ค๊ฒŒ ๋˜๊ณ , ์ดํ›„ ๋‘ ๋ฒˆ ์žฌ์‹œ๋„ํ•œ๋‹ค.

๋‘ ๋ฒˆ์˜ ์žฌ์‹œ๋„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์—๋Ÿฌ๊ฐ€ ๋‚ด๋ ค์˜ค๋ฏ€๋กœ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์—๋Ÿฌ๋ฅผ ๋‚ธ๋‹ค.

RxSwift

retry ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

Observable<Void>.error(error)
  .retry(2)
  .subscribe(onNext: { value in
    print("RxSwift Retry : \(value)")
  }, onError: { _ in
    print("RxSwift Retry Error")
  }, onCompleted: {
    print("RxSwift Retry Finish")
  })
  .disposed(by: disposeBag)

// RxSwift Retry Error

ReactiveSwift

retry ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

SignalProducer<Void, Error>(error: error)
  .retry(upTo: 2)
  .start { event in
    switch event {
    case let .value(value):
      print("ReactiveSwift Retry : \(value)")
    case .failed:
      print("ReactiveSwift Retry Error")
    case .completed:
      print("ReactiveSwift Retry Finish")
    default:
      break
    }
  }

// ReactiveSwift Retry Error

์ฐธ๊ณ 

ReactiveX - Operators - Retry

Last updated

Was this helpful?