Publishers.First
์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์คํธ๋ฆผ์ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฐํํ๊ณ ์ข ๋ฃํ๋ Publisher
์ด๋์ ๋ผ์ด์ ๋ ํ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
upstream: ์์์ ํ๋ฅด๋ Publisher
first ์คํผ๋ ์ดํฐ์ ๊ด๋ จ์ด ์๋ค.
// Publishers.First Publisher
Publishers
.First(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3, 4, 5]))
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine First Error")
case .finished:
print("Combine First Finish")
}
}, receiveValue: { value in
print("Combine First : \(value)")
})
.store(in: &cancellables)
// first Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3, 4, 5])
.first()
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine First Error")
case .finished:
print("Combine First Finish")
}
}, receiveValue: { value in
print("Combine First : \(value)")
})
.store(in: &cancellables)
// Combine First : 1
// Combine First Finish์์ Publisher๋ 1, 2, 3, 4, 5์ ๊ฐ์ ์ฐจ๋ก๋๋ก ๋ธ๋ค.
first์ ์ํด ์ฒซ ๋ฒ์งธ ์์๋ง ๋ฐํ๋๋ฉฐ, ๊ฒฐ๊ณผ์ ์ผ๋ก 1์ ๊ฐ์ ๋ด๊ณ ์ข
๋ฃํ๋ค.
RxSwift
first ์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
Observable.from([1, 2, 3, 4, 5])
.first()
.subscribe(onSuccess: { value in
print("RxSwift First : \(value)")
}, onError: { _ in
print("RxSwift First Error")
})
.disposed(by: disposeBag)
// RxSwift First : Optional(1)ReactiveSwift
first ์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
SignalProducer([1, 2, 3])
.first()?.signal
.observe { event in
switch event {
case let .value(value):
print("ReactiveSwift First : \(value)")
case .failed:
print("ReactiveSwift First Error")
case .completed:
print("ReactiveSwift First Finish")
default:
break
}
}
// ReactiveSwift First : 1
// ReactiveSwift First Finish์ฐธ๊ณ
Last updated
Was this helpful?