μ λ€λ¦ ꡬ쑰체 | μ€νΈλ¦Όμ΄ μ’
λ£ν ν μ€νΈλ¦Όμ λ§μ§λ§ μμλ§μ λ°ννλ 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
μ°Έκ³
ReactiveX - Operators - Last