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