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 ErrorJust Publisher๋ ์๋ฌ๋ฅผ ๋ด์ง ์๋ Publisher์ด์ง๋ง Fail Publisher์ ์กฐํฉํ๊ธฐ ์ํด Fail Publisher์ ์๋ฌ ํ์
์ธ Error ํ์
์ ์๋ฌ๋ก ํ์
์ ๋ง์ถ๊ธฐ ์ํด setFailureType์ด ์๊ตฌํ๋ ์๋ฌ์ ํ์
์ Error.self๋ฅผ ๋ช
์ํด ์ฃผ์๋ค.
์ด๋ฅผ ํตํด Just Publisher์ Fail Publisher๋ ์กฐํฉ์ด ๊ฐ๋ฅํ๊ฒ ๋์๋ค.
์ฝ๋ ์คํ ๊ฒฐ๊ณผ๋ก, Fail Publisher๊ฐ ๋ด๋ ์๋ฌ๊ฐ ์ฒ๋ฆฌ๋์ง ์์๊ธฐ ๋๋ฌธ์ ์กฐํฉ๋ Publisher๋ ์๋ฌ๋ฅผ ๋ด๊ฒ ๋๋ค.
RxSwift
Observable์ด ์๋ฌ์ ํ์ ์ ์๊ตฌํ์ง ์์ผ๋ฏ๋ก ์ด๋ฌํ ๋์์ ๊ตฌํํ ํ์๊ฐ ์๊ณ , ๊ตฌํ๋์ด ์์ง๋ ์๋ค.
ReactiveSwift
promoteError ์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
Last updated
Was this helpful?