μ λ€λ¦ ꡬ쑰체 | μ€ν¨νλ Publisherλ₯Ό λ€λ₯Έ Publisherλ‘ κ΅μ²΄νμ¬ μμμ νλ₯΄λ Publisherλ‘λΆν° μλ¬λ₯Ό μ²λ¦¬νλ Publisher
μ΄λμ
λΌμ΄μ λ λ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher
handler
: μμμ νλ₯΄λ Publisherκ° μλ¬λ₯Ό λ΄λ κ²½μ° μλ‘μ΄ Publisherλ₯Ό λ§λλ ν΄λ‘μ
μμμ νλ₯΄λ Publisherμ μλ‘μ΄ Publisherμ Output νμ
μ λμΌν΄μΌ νλ€.
μμ Publisherκ° μλ¬λ₯Ό λ΄λ κ²½μ° μλ¬λ₯Ό λ΄λ©° μ’
λ£νλ λμ λ€λ₯Έ Publisherλ‘ λ체νλ€.
catch
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
// Publishers.Catch Publisher
Publishers.Catch(upstream: Fail(error: error)) { _ in Just("Error") }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Catch Error")
case .finished:
print("Combine Catch Finish")
}
}, receiveValue: {
print("Combine Catch : \($0)")
})
.store(in: &cancellables)
// catch Operator
Fail(error: error)
.catch { _ in Just("Error") }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Catch Error")
case .finished:
print("Combine Catch Finish")
}
}, receiveValue: {
print("Combine Catch : \($0)")
})
.store(in: &cancellables)
// Combine Catch : Error
// Combine Catch Finish
μμ μ½λλ μ€ν¨νλ PublisherμΈ Fail
λ‘λΆν° catch
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ μλ¬λ₯Ό λ΄κ³ μ’
λ£νλ λμ Just
Publisherλ‘ λ체νλ€.
RxSwift
catchError
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
Observable.error(error)
.catchError { _ in Observable.just("Error") }
.subscribe(onNext: {
print("RxSwift Catch : \($0)")
}, onError: { _ in
print("RxSwift Catch Error")
}, onCompleted: {
print("RxSwift Catch Finish")
})
.disposed(by: disposeBag)
// RxSwift Catch : Error
// RxSwift Catch Finish
ReactiveSwift
flatMapError
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
SignalProducer(error: error)
.flatMapError { error in SignalProducer(value: "Error") }
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift Catch : \(value)")
case .failed:
print("ReactiveSwift Catch Error")
case .completed:
print("ReactiveSwift Catch Finish")
default:
break
}
}
// ReactiveSwift Catch : Error
// ReactiveSwift Catch Finish
μ°Έκ³
ReactiveX - Operators - Catch