Publishers.PrefixUntilOutput
์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด
์ด๋์ ๋ผ์ด์ ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
upstream
: ์์์ ํ๋ฅด๋ Publisherother
: ์ฒซ ์ถ๋ ฅ์ดupstream
Publisher๋ฅผ ์ข ๋ฃํ๊ฒ ํ๋ Publisher
๋ค๋ฅธ Publisher๊ฐ ๊ฐ์ ๋ผ ๋๊น์ง ๊ธฐ์กด Publisher๊ฐ ์์๋ฅผ ๋ฐํํ๋ ๋์์ ๊ตฌํํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
prefix
์คํผ๋ ์ดํฐ์ ๊ด๋ จ์ด ์๋ค.
let sourceSubject = PassthroughSubject<Int, Never>()
let otherSubject = PassthroughSubject<Int, Never>()
// Publishers.PrefixUntilOutput Publisher
Publishers
.PrefixUntilOutput(upstream: sourceSubject, other: otherSubject)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine PrefixUntilOutput Error")
case .finished:
print("Combine PrefixUntilOutput Finish")
}
}, receiveValue: { value in
print("Combine PrefixUntilOutput : \(value)")
})
.store(in: &cancellables)
// prefix Operator
sourceSubject
.prefix(untilOutputFrom: otherSubject)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine PrefixUntilOutput Error")
case .finished:
print("Combine PrefixUntilOutput Finish")
}
}, receiveValue: { value in
print("Combine PrefixUntilOutput : \(value)")
})
.store(in: &cancellables)
// 1
sourceSubject.send(1)
// 2
otherSubject.send(2)
// 3
sourceSubject.send(3)
// Combine PrefixUntilOutput : 1
// Combine PrefixUntilOutput Finish
์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ์์๋ก ๋์ํ๋ค.
1์ ์ฝ๋๋ฅผ ์คํํ๋ฉด
sourceSubject
์ 1์ ๊ฐ์ ์ ๋ฌํ๋ฉฐ,otherSubject
๊ฐ ๊ฐ์ ๋ฐํํ ์ ์ด ์์ผ๋ฏ๋ก ๊ทธ๋๋ก 1์ ๊ฐ์ ๋ธ๋ค.2์ ์ฝ๋๋ฅผ ์คํํ๋ฉด
otherSubject
์ 2์ ๊ฐ์ ์ ๋ฌํ๋ค. ์ด ๋sourceSubject
๋ ๋ค๋ฅธ Publisher๊ฐ ๊ฐ์ ์ ๋ฌ๋ฐ์์ผ๋ฏ๋ก ์ข ๋ฃํ๋ค.3์ ์ฝ๋๋ฅผ ์คํํ๋ฉด
sourceSubject
์ 3์ ๊ฐ์ ์ ๋ฌํ๋ค. ํ์ง๋งsourceSubject
๋ 4์ ์ฝ๋์ ์คํ์ ์ํด ์ด๋ฏธ ์ข ๋ฃํ์์ผ๋ฏ๋ก ํจ๋ ฅ์ด ์๋ค.
RxSwift
takeUntil
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
let sourceSubject = PublishSubject<Int>()
let otherSubject = PublishSubject<Int>()
sourceSubject
.takeUntil(otherSubject)
.subscribe(onNext: { value in
print("RxSwift PrefixUntilOutput : \(value)")
}, onError: { _ in
print("RxSwift PrefixUntilOutput Error")
}, onCompleted: {
print("RxSwift PrefixUntilOutput Finish")
})
.disposed(by: disposeBag)
sourceSubject.onNext(1)
otherSubject.onNext(2)
sourceSubject.onNext(3)
// RxSwift PrefixUntilOutput : 1
// RxSwift PrefixUntilOutput Finish
ReactiveSwift
take
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
let sourceProperty = MutableProperty(0)
let otherProperty = MutableProperty(0)
sourceProperty.signal
.take(until: otherProperty.signal.map(value: Void()))
.observe { event in
switch event {
case let .value(value):
print("ReactiveSwift PrefixUntilOutput : \(value)")
case .failed:
print("ReactiveSwift PrefixUntilOutput Error")
case .completed:
print("ReactiveSwift PrefixUntilOutput Finish")
default:
break
}
}
sourceProperty.value = 1
otherProperty.value = 2
sourceProperty.value = 3
// ReactiveSwift PrefixUntilOutput : 1
// ReactiveSwift PrefixUntilOutput Finish
์ฐธ๊ณ
Last updated
Was this helpful?