Publishers.Collect
μ λ€λ¦ ꡬ쑰체 | μμ΄ν μ μΌμμ μΌλ‘ κΈ°μ΅νλ 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 Error1μ κ²½μ° μλ£ μ΄λ²€νΈλ₯Ό λ΄κΈ°κΉμ§ μ λ¬λ 1, 2μ κ°μ΄ λ°°μ΄μ ννλ‘ λ¬Άμ¬ νλ²μ λ°νλλ€.
2μ κ²½μ° μλ¬λ‘ μΈν΄ μ’ λ£νκΈ° λλ¬Έμ κ²°κ³Όμ μΌλ‘ μλ¬λ₯Ό λΈλ€.
RxSwift
toArray μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
ReactiveSwift
collect μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
μ°Έκ³
Last updated
Was this helpful?