Publishers.Output
์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ๋ฐํ๋ ์์์ ์ํ์ค ๋ด์์ ๋ฒ์๋ก ์ง์ ๋ ์์๋ค์ ๋ฐํํ๋ Publisher
์ด๋์ ๋ผ์ด์ ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
upstream
: ์์์ ํ๋ฅด๋ Publisherrange
: ๋ฐํํ ์์๊ฐ ์ํ ๋ฒ์
output
์คํผ๋ ์ดํฐ์ ๊ด๋ จ์ด ์๋ค.
// 1 : Publishers.Output Publisher
Publishers
.Output(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3]), range: 0 ..< 2)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Output Error")
case .finished:
print("Combine Output Finish")
}
}, receiveValue: { value in
print("Combine Output : \(value)")
})
.store(in: &cancellables)
// Combine Output : 1
// Combine Output : 2
// Combine Output Finish
// 2 : prefix Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.prefix(2)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Output Error")
case .finished:
print("Combine Output Finish")
}
}, receiveValue: { value in
print("Combine Output : \(value)")
})
.store(in: &cancellables)
// Combine Output : 1
// Combine Output : 2
// Combine Output Finish
// 3 : output Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.output(at: 1)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Output Error")
case .finished:
print("Combine Output Finish")
}
}, receiveValue: { value in
print("Combine Output : \(value)")
})
.store(in: &cancellables)
// Combine Output : 2
// Combine Output Finish
// 4 : output Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.output(in: 0 ..< 2)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Output Error")
case .finished:
print("Combine Output Finish")
}
}, receiveValue: { value in
print("Combine Output : \(value)")
})
.store(in: &cancellables)
// Combine Output : 1
// Combine Output : 2
// Combine Output Finish
๊ฐ ์ฝ๋์ ์์ Publisher๋ 1, 2, 3์ ๊ฐ์ ์ฐจ๋ก๋๋ก ๋ธ๋ค.
1์ ์ฝ๋๋ 0 ..< 2
์ ๋ฒ์์ ์ํด ํด๋น ๋ฒ์์ ์๋ 1, 2์ ๊ฐ์ ๋ด๊ณ ์ข
๋ฃํ๋ค.
2์ ์ฝ๋๋ prefix(2)
์ ์ํด ์ฌ์ฉํ์ฌ ์์์๋ถํฐ ๋ ๊ฐ์ ์์๋ฅผ ๋ฐํํ๋ค. ๋ฐ๋ผ์ 1, 2์ ๊ฐ์ ๋ด๊ณ ์ข
๋ฃํ๋ค.
3์ ์ฝ๋๋ output(at: 1)
์ ์ํด ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค์ ์๋ 2์ ๊ฐ์ ๋ด๊ณ ์ข
๋ฃํ๋ค.
4์ ์ฝ๋๋ output(in: 0 ..< 2)
์ ์ํด ๋ฒ์์ ์๋ 1, 2์ ๊ฐ์ ๋ด๊ณ ์ข
๋ฃํ๋ค.
RxSwift
elementAt
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
Observable.from([1, 2, 3])
.elementAt(1)
.subscribe(onNext: { value in
print("RxSwift Output : \(value)")
}, onError: { _ in
print("RxSwift Output Error")
}, onCompleted: {
print("RxSwift Output Finish")
})
.disposed(by: disposeBag)
// RxSwift Output : 2
// RxSwift Output Finish
ReactiveSwift
ํด๋น ๋์์ ๊ตฌํํ๊ธฐ ์ํ ์คํผ๋ ์ดํฐ๋ฅผ ์ ๊ณตํ์ง ์๋๋ค.
์ฐธ๊ณ
Last updated
Was this helpful?