์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์ฃผ์ด์ง ํด๋ก์ ๊ฐ false๋ฅผ ๋ฐํํ ๋๊น์ง ์์์ ํ๋ฅด๋ Publisher๋ก๋ถํฐ ์์๋ฅผ ์๋ตํ๋ Publisher
์ด๋์
๋ผ์ด์ ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
upstream
: ์์์ ํ๋ฅด๋ Publisher
predicate
: ์์์ ์๋ต ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ ํด๋ก์
์กฐ๊ฑด์ ๋ง์ง ์์ ๋๊น์ง๋ ๋ฐํ๋ ๊ฐ์ ๋ฌด์ํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
drop
์คํผ๋ ์ดํฐ์ ๊ด๋ จ์ด ์๋ค.
// Publishers.DropWhile Publisher
Publishers
.DropWhile(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])) { $0 < 3 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine DropWhile Error")
case .finished:
print("Combine DropWhile Finish")
}
}, receiveValue: { value in
print("Combine DropWhile : \(value)")
})
.store(in: &cancellables)
// drop Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.drop { $0 < 3 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine DropWhile Error")
case .finished:
print("Combine DropWhile Finish")
}
}, receiveValue: { value in
print("Combine DropWhile : \(value)")
})
.store(in: &cancellables)
// Combine DropWhile : 3
// Combine DropWhile Finish
์์ Publisher๊ฐ 1, 2, 3์ ๊ฐ์ ์์๋๋ก ๋ธ๋ค.
3 ๋ฏธ๋ง์ผ ๋๊น์ง ๋ฐํ๋ ๊ฐ์ ์๋ตํ๋ผ๋ ์กฐ๊ฑด์ ์ํด 3์ ๊ฐ์ ๋ด๊ณ ์ข
๋ฃํ๋ค.
RxSwift
skipWhile
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
Observable.from([1, 2, 3])
.skipWhile { $0 < 3 }
.subscribe(onNext: { value in
print("RxSwift DropWhile : \(value)")
}, onError: { _ in
print("RxSwift DropWhile Error")
}, onCompleted: {
print("RxSwift DropWhile Finish")
})
.disposed(by: disposeBag)
// RxSwift DropWhile : 3
// RxSwift DropWhile Finish
ReactiveSwift
skip
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
SignalProducer([1, 2, 3])
.skip { $0 < 3 }
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift DropWhile : \(value)")
case .failed:
print("ReactiveSwift DropWhile Error")
case .completed:
print("ReactiveSwift DropWhile Finish")
default:
break
}
}
// ReactiveSwift DropWhile : 3
// ReactiveSwift DropWhile Finish
์ฐธ๊ณ
ReactiveX - Operators - Skip