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