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