μ λ€λ¦ ꡬ쑰체 | μμ΄ν
μ μΌμμ μΌλ‘ κΈ°μ΅νλ Publisher
μ΄λμ
λΌμ΄μ λ ν κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher
κ°μ λ°μ λλ§λ€ λμνμ§ μκ³ , μλ¬ λλ μ’
λ£ μ΄λ²€νΈκ° λ λκΉμ§ κ°μ κΈ°μ΅ν΄ λμλ€κ° λ°°μ΄μ ννλ‘ κ°μ νκΊΌλ²μ λΈλ€.
μλ¬λ₯Ό λ΄λ©΄ κ·Έ μμ κΉμ§ λͺ¨μ λμ κ°λ€ λμ μλ¬λ₯Ό λΈλ€.
μ’
λ£λ₯Ό λ΄λ©΄ κ·Έ μμ κΉμ§ λͺ¨μ λμ κ°λ€μ λ°°μ΄μ ννλ‘ νκΊΌλ²μ λΈλ€.
collect
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
let subject = PassthroughSubject<Int, Error>()
// Publishers.Collect Publisher
Publishers.Collect(upstream: subject)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Collect Error")
case .finished:
print("Combine Collect Finish")
}
}, receiveValue: { value in
print("Combine Collect : \(value)")
})
.store(in: &cancellables)
// collect Operator
subject
.collect()
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Collect Error")
case .finished:
print("Combine Collect Finish")
}
}, receiveValue: { value in
print("Combine Collect : \(value)")
})
.store(in: &cancellables)
// 1
subject.send(1)
subject.send(2)
subject.send(completion: .finished)
// Combine Collect : [1, 2]
// Combine Collect Finish
// 2
subject.send(1)
subject.send(completion: .failure(error))
// Combine Collect Error
1μ κ²½μ° μλ£ μ΄λ²€νΈλ₯Ό λ΄κΈ°κΉμ§ μ λ¬λ 1, 2μ κ°μ΄ λ°°μ΄μ ννλ‘ λ¬Άμ¬ νλ²μ λ°νλλ€.
2μ κ²½μ° μλ¬λ‘ μΈν΄ μ’
λ£νκΈ° λλ¬Έμ κ²°κ³Όμ μΌλ‘ μλ¬λ₯Ό λΈλ€.
RxSwift
toArray
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
let subject = PublishSubject<Int>()
subject
.toArray()
.subscribe(onSuccess: { value in
print("RxSwift Collect : \(value)")
}, onError: { _ in
print("RxSwift Collect Error")
})
.disposed(by: disposeBag)
subject.onNext(1)
subject.onNext(2)
subject.onCompleted()
// RxSwift Collect : [1, 2]
ReactiveSwift
collect
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
let property = MutableProperty<Int>(0)
property.signal
.collect()
.observe { event in
switch event {
case let .value(value):
print("ReactiveSwift Collect : \(value)")
case .failed:
print("ReactiveSwift Collect Error")
case .completed:
print("ReactiveSwift Collect Finish")
default:
break
}
}
property.value = 1
property.value = 2
// ReactiveSwift Collect : [1, 2]
// ReactiveSwift Collect Finish
μ°Έκ³
ReactiveX - Operators - To