Publishers.ReplaceError

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์ŠคํŠธ๋ฆผ์— ์žˆ๋Š” ์–ด๋– ํ•œ ์—๋Ÿฌ๋ฅผ ์ œ๊ณต๋œ ์š”์†Œ๋กœ ๊ต์ฒดํ•˜๋Š” Publisher

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

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

  • output : ์ƒ์œ„์— ํ๋ฅด๋Š” Publisher๋กœ๋ถ€ํ„ฐ ์—๋Ÿฌ๋ฅผ ๋Œ€์ฒดํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์š”์†Œ

Publishers.Catch๊ฐ€ ์ƒ์œ„ Publisher๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋‚ผ ๋•Œ ๋‹ค๋ฅธ Publisher๋กœ ๊ต์ฒดํ•˜๋Š” ๋™์ž‘์„ ์ œ๊ณตํ•œ๋‹ค๋ฉด, Publishers.ReplaceError๋Š” ์ƒ์œ„ Publisher๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋‚ผ ๋•Œ ํŠน์ • ์š”์†Œ๋กœ ๊ต์ฒดํ•˜๋Š” ๋™์ž‘์„ ์ œ๊ณตํ•œ๋‹ค.

Publishers.Catch์˜ ๋™์ž‘๋ณด๋‹ค ๋”์šฑ ๊ฐ„๋‹จํ•˜๊ฒŒ ์—๋Ÿฌ๋ฅผ ๋‚ด๋Š” Publisher๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

// Publishers.ReplaceError Publisher
Publishers.ReplaceError(upstream: Fail(error: error), output: Void())
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine ReplaceError Error")
    case .finished:
      print("Combine ReplaceError Finish")
    }
  }, receiveValue: {
    print("Combine ReplaceError")
  })
  .store(in: &cancellables)

// replaceError Operator
Fail(error: error)
  .replaceError(with: Void())
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine ReplaceError Error")
    case .finished:
      print("Combine ReplaceError Finish")
    }
  }, receiveValue: {
    print("Combine ReplaceError")
  })
  .store(in: &cancellables)

// Combine ReplaceError
// Combine ReplaceError Finish

์ƒ์œ„ Publisher๊ฐ€ Fail์ด์–ด์„œ ์—๋Ÿฌ๋ฅผ ๋‚ด๋Š” ๊ฒƒ์ธ๋ฐ, replaceError๋ฅผ ํ†ตํ•ด Void ๊ฐ’์œผ๋กœ ์—๋Ÿฌ๋ฅผ ๋Œ€์ฒดํ•œ๋‹ค.

RxSwift

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

Observable.error(error)
  .catchErrorJustReturn(Void())
  .subscribe(onNext: {
    print("RxSwift ReplaceError")
  }, onError: { _ in
    print("RxSwift ReplaceError Error")
  }, onCompleted: {
    print("RxSwift ReplaceError Finish")
  })
  .disposed(by: disposeBag)

// RxSwift ReplaceError
// RxSwift ReplaceError Finish

ReactiveSwift

ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ฐธ๊ณ 

ReactiveX - Operators - Catch

Publishers.Catch

Last updated

Was this helpful?