Publishers.ReplaceEmpty
์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ๋น์ด ์๋ ์คํธ๋ฆผ์ ์ ๊ณต๋ ์์๋ก ๊ต์ฒดํ๋ Publisher
์ด๋์ ๋ผ์ด์ ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
upstream: ์์์ ํ๋ฅด๋ Publisheroutput: ์์์ ํ๋ฅด๋ Publisher๊ฐ ์ด๋ ํ ์์๋ ์ ๋ฌํ์ง ์๊ณ ์ข ๋ฃํ ๋ ์ ๋ฌํ ์์
์์ Publisher๊ฐ ์ฆ์ ์ข
๋ฃํ๋ Empty์ ๊ฐ์ ๊ฒ์ด๋ ์ํ์ค๊ฐ ๋น์ด ์๋ Publishers.Sequence ๋ฑ์ธ ๊ฒฝ์ฐ์ ํด๋น Publisher์ ๋ช
์๋ ๊ฐ์ผ๋ก ๊ต์ฒดํ๋ค.
๊ฐ์ ๋ด์ง ์๊ณ ์ข ๋ฃํ๋ Publisher๊ฐ ํน์ ๊ฐ์ ๋ด๊ณ ์ข ๋ฃํ ์ ์๊ฒ ํ๊ธฐ ์ํด ์ฌ์ฉํ ์ ์๋ค.
replaceEmpty ์คํผ๋ ์ดํฐ์ ๊ด๋ จ์ด ์๋ค.
// Publishers.ReplaceEmpty Publisher
Publishers.ReplaceEmpty(upstream: Publishers.Sequence<[Int], Never>(sequence: []), output: 0)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine ReplaceEmpty Error")
case .finished:
print("Combine ReplaceEmpty Finish")
}
}, receiveValue: { value in
print("Combine ReplaceEmpty : \(value)")
})
.store(in: &cancellables)
// replaceEmpty Operator
Publishers.Sequence<[Int], Never>(sequence: [])
.replaceEmpty(with: 0)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine ReplaceEmpty Error")
case .finished:
print("Combine ReplaceEmpty Finish")
}
}, receiveValue: { value in
print("Combine ReplaceEmpty : \(value)")
})
.store(in: &cancellables)
// Combine ReplaceEmpty : 0
// Combine ReplaceEmpty Finish์์ ์ฝ๋ ๋ชจ๋ Publishers.Sequence Publisher์ ๋น ๋ฐฐ์ด์ ๋ฃ์ด ๊ฐ์ ๋ด์ง ์๊ณ ์ข
๋ฃํ๋ Publisher๋ฅผ ๋ง๋ ๋ค.
replaceEmpty์ ์ํด ์ด Publisher๋ ์ฃผ์ด์ง ๊ฐ์ผ๋ก ๋์ฒด๋ ์ ์๋ค.
RxSwift
ifEmpty ์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
Observable.from([])
.ifEmpty(default: 0)
.subscribe(onNext: { value in
print("RxSwift ReplaceEmpty : \(value)")
}, onError: { _ in
print("RxSwift ReplaceEmpty Error")
}, onCompleted: {
print("RxSwift ReplaceEmpty Finish")
})
.disposed(by: disposeBag)
// RxSwift ReplaceEmpty : 0
// RxSwift ReplaceEmpty FinishReactiveSwift
ํด๋น ๊ธฐ๋ฅ์ ๊ตฌํํ๊ธฐ ์ํ ์คํผ๋ ์ดํฐ๋ฅผ ์ ๊ณตํ์ง ์๋๋ค.
์ฐธ๊ณ
Last updated
Was this helpful?