Publishers.SetFailureType

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ํŠน์ • ์‹คํŒจ ํƒ€์ž…์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” Publisher

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

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

์ƒ์œ„ Publisher์˜ ์—๋Ÿฌ ํƒ€์ž…์€ Never์ด๋ฉฐ, ํ•ด๋‹น Publisher์˜ ์—๋Ÿฌ ํƒ€์ž…์€ Error ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•ด์•ผ ํ•œ๋‹ค.

์ด๋Š” ์‹ค์ œ๋กœ ํŠน์ • ํƒ€์ž…์˜ ์—๋Ÿฌ๋ฅผ ๋‚ด์ง€๋Š” ์•Š๊ณ  ์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒํ•˜์ง€๋งŒ, ๋‹ค๋ฅธ ์—๋Ÿฌ ํƒ€์ž…์ด ๋งž์ง€ ์•Š๋Š” Publisher์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์—๋Ÿฌ์˜ ํƒ€์ž…์„ ์ผ์น˜์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด 1๋ฒˆ Publisher๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋‚ด์ง€ ์•Š๊ณ  (์—๋Ÿฌ์˜ ํƒ€์ž…์ด Never) 2๋ฒˆ Publisher๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋‚ด๋Š”๋ฐ ๋‘ ๊ฐœ์˜ Publisher๋ฅผ ์กฐํ•ฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด 1๋ฒˆ Publisher๊ฐ€ 2๋ฒˆ Publisher์˜ ์—๋Ÿฌ ํƒ€์ž…์˜ ์—๋Ÿฌ๋ฅผ ๋‚ด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ–‰๋™ํ•˜๋„๋ก ํ•˜์—ฌ ํƒ€์ž…์„ ๋งž์ถ”์–ด ์ฃผ์–ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

์ด ๋•Œ ํ•ด๋‹น Publisher๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹ค์ œ๋กœ ํŠน์ • ํƒ€์ž…์˜ ์—๋Ÿฌ๋ฅผ ๋‚ด์ง€๋Š” ์•Š์ง€๋งŒ ์—๋Ÿฌ์˜ ํƒ€์ž…์„ ๋งž์ถฐ์ค„ ์ˆ˜ ์žˆ๋‹ค.

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

// Publishers.SetFailureType Publisher
Just(Void())
  .setFailureType(to: Error.self)
  .combineLatest(Fail<Void, Error>(error: error))
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine SetFailureType Error")
    case .finished:
      print("Combine SetFailureType Finish")
    }
  }, receiveValue: { _ in
    print("Combine SetFailureType")
  })
  .store(in: &cancellables)

// setFailureType Operator
Publishers.SetFailureType<Just<Void>, Error>(upstream: Just(Void()))
  .combineLatest(Fail<Void, Error>(error: error))
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine SetFailureType Error")
    case .finished:
      print("Combine SetFailureType Finish")
    }
  }, receiveValue: { _ in
    print("Combine SetFailureType")
  })
  .store(in: &cancellables)

// Combine SetFailureType Error

Just Publisher๋Š” ์—๋Ÿฌ๋ฅผ ๋‚ด์ง€ ์•Š๋Š” Publisher์ด์ง€๋งŒ Fail Publisher์™€ ์กฐํ•ฉํ•˜๊ธฐ ์œ„ํ•ด Fail Publisher์˜ ์—๋Ÿฌ ํƒ€์ž…์ธ Error ํƒ€์ž…์˜ ์—๋Ÿฌ๋กœ ํƒ€์ž…์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด setFailureType์ด ์š”๊ตฌํ•˜๋Š” ์—๋Ÿฌ์˜ ํƒ€์ž…์— Error.self๋ฅผ ๋ช…์‹œํ•ด ์ฃผ์—ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด Just Publisher์™€ Fail Publisher๋Š” ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

์ฝ”๋“œ ์‹คํ–‰ ๊ฒฐ๊ณผ๋กœ, Fail Publisher๊ฐ€ ๋‚ด๋Š” ์—๋Ÿฌ๊ฐ€ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐํ•ฉ๋œ Publisher๋Š” ์—๋Ÿฌ๋ฅผ ๋‚ด๊ฒŒ ๋œ๋‹ค.

RxSwift

Observable์ด ์—๋Ÿฌ์˜ ํƒ€์ž…์„ ์š”๊ตฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ด๋Ÿฌํ•œ ๋™์ž‘์„ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†๊ณ , ๊ตฌํ˜„๋˜์–ด ์žˆ์ง€๋„ ์•Š๋‹ค.

ReactiveSwift

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

SignalProducer(value: Void())
  .promoteError(Error.self)
  .combineLatest(with: SignalProducer<Void, Error>(error: error))
  .start { event in
    switch event {
    case .value:
      print("ReactiveSwift SetFailureType")
    case .failed:
      print("ReactiveSwift SetFailureType")
    case .completed:
      print("ReactiveSwift SetFailureType")
    default:
      break
    }
  }

// ReactiveSwift SetFailureType Error

Last updated

Was this helpful?