Publishers.MapError
μ λ€λ¦ ꡬ쑰체 | μμμ νλ₯΄λ Publisherμ μ΄λ ν μ€ν¨λ₯Ό μλ‘μ΄ μλ¬λ‘ λ³ννλ Publisher
μ΄λμ
λΌμ΄μ λ λ κ°μ μΈμλ₯Ό λ°λλ€.
upstream : μμμ νλ₯΄λ Publisher
transform : μμμ νλ₯΄λ Publisherμ μ€ν¨λ₯Ό μλ‘μ΄ μλ¬λ‘ λ³ννλ ν΄λ‘μ
upstream Publisherμ μλ¬ νμ
μ Errorλ€.
μλ¬λ₯Ό λ³ννκΈ° μν΄ μ¬μ©νλ€. μλ¬λ₯Ό μ²λ¦¬νλ κ²μ΄ μλλ―λ‘ μμμ νλ₯΄λ Publisherκ° μλ¬λ₯Ό λ΄κ³ λ³λμ μλ¬ μ²λ¦¬ μμ
μ΄ μλ€λ©΄ μλ¬λ₯Ό λ΄λ©° μ’
λ£νκ² λλ€.
mapError μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
// Publishers.MapError Publisher
Publishers.MapError(upstream: Fail<Void, NSError>(error: error), { $0 as Error })
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine MapError Error")
case .finished:
print("Combine MapError Finish")
}
}, receiveValue: {
print("Combine MapError")
})
.store(in: &cancellables)
// mapError Operator
Fail(error: error)
.mapError { $0 as Error }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine MapError Error")
case .finished:
print("Combine MapError Finish")
}
}, receiveValue: {
print("Combine MapError")
})
.store(in: &cancellables)
// Combine MapError Error
λ μ½λ λͺ¨λ μμ Publisherλ‘ NSError κ°μ²΄λ₯Ό λ°λ Fail Publisherλ₯Ό μ¬μ©νμλ€.
$0 as Errorλ₯Ό ν΅ν΄ NSError νμ
μ΄ Error νμ
μΌλ‘ λ§€νλλ€.
μμμ νλ₯΄λ Publisherκ° μλ¬λ₯Ό λ΄λ―λ‘ κ²°κ³Όμ μΌλ‘ μλ¬λ₯Ό λ΄λ©° μ’
λ£νλ€.
λ΄λΆμ μΌλ‘ μλ¬μ νμ
μΌλ‘ Errorλ₯Ό λ
ΈμΆνλ―λ‘ mapErrorμ κ°μ΄ μλ¬μ νμ
μ λ³ννλ μ€νΌλ μ΄ν°λ₯Ό μ 곡νμ§ μλλ€.
mapError μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.