μ λ€λ¦ ꡬ쑰체 | λ κ°μ μμ Publisherμ κ²°ν© ν¨μλ₯Ό μ μ©νμ¬ μμ±λλ Publisher
μ΄λμ
λΌμ΄μ λ μ‘°ν©ν λ κ°μ Publisherλ₯Ό λ°λλ€.
μΈμμ λ€μ΄κ°λ λͺ¨λ Publisherμ Output νμ
κ³Ό μλ¬ νμ
μ κ°μμΌ νλ€.
λ κ°μ μ€νΈλ¦Όμ νλλ‘ ν©μ³ νλμ μ€νΈλ¦Όμ λ€λ£¨λ κ²μ²λΌ νκΈ° μν΄ μ¬μ©ν μ μλ€.
merge
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
let aSubject = PassthroughSubject<Int, Never>()
let bSubject = PassthroughSubject<Int, Never>()
// Publishers.Merge Publisher
Publishers
.Merge(aSubject, bSubject)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Merge Error")
case .finished:
print("Combine Merge Finish")
}
}, receiveValue: { value in
print("Combine Merge : \(value)")
})
.store(in: &cancellables)
// merge Operator
aSubject
.merge(with: bSubject)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Merge Error")
case .finished:
print("Combine Merge Finish")
}
}, receiveValue: { value in
print("Combine Merge : \(value)")
})
.store(in: &cancellables)
// 1
aSubject.send(1)
// 2
aSubject.send(2)
// 3
bSubject.send(3)
// 4
bSubject.send(4)
// Combine Merge : 1
// Combine Merge : 2
// Combine Merge : 3
// Combine Merge : 4
μ½λλ λ€μκ³Ό κ°μ΄ λμνλ€.
1μ μ½λλ₯Ό μ€ννμ¬ aSubject
μ 1μ κ°μ μ λ¬νλ€. ν΄λΉ Publisherλ 1μ κ°μ λΈλ€.
2μ μ½λλ₯Ό μ€ννμ¬ aSubject
μ 2μ κ°μ μ λ¬νλ€. ν΄λΉ Publisherλ 2μ κ°μ λΈλ€.
3μ μ½λλ₯Ό μ€ννμ¬ bSubject
μ 3μ κ°μ μ λ¬νλ€. ν΄λΉ Publisherλ 3μ κ°μ λΈλ€.
4μ μ½λλ₯Ό μ€ννμ¬ bSubject
μ 4μ κ°μ μ λ¬νλ€. ν΄λΉ Publisherλ 4μ κ°μ λΈλ€.
RxSwift
merge
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
let aSubject = PublishSubject<Int>()
let bSubject = PublishSubject<Int>()
Observable.merge(aSubject, bSubject)
.subscribe(onNext: { value in
print("RxSwift Merge : \(value)")
}, onError: { _ in
print("RxSwift Merge Error")
}, onCompleted: {
print("RxSwift Merge Finish")
})
.disposed(by: disposeBag)
aSubject.onNext(1)
aSubject.onNext(2)
bSubject.onNext(3)
bSubject.onNext(4)
// RxSwift Merge : 1
// RxSwift Merge : 2
// RxSwift Merge : 3
// RxSwift Merge : 4
ReactiveSwift
merge
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
let aProperty = MutableProperty<Int>(0)
let bProperty = MutableProperty<Int>(0)
aProperty.signal
.merge(with: bProperty.signal)
.observe { event in
switch event {
case let .value(value):
print("ReactiveSwift Merge : \(value)")
case .failed:
print("ReactiveSwift Merge Error")
case .completed:
print("ReactiveSwift Merge Finish")
default:
break
}
}
aProperty.value = 1
aProperty.value = 2
bProperty.value = 3
bProperty.value = 4
// ReactiveSwift Merge : 1
// ReactiveSwift Merge : 2
// ReactiveSwift Merge : 3
// ReactiveSwift Merge : 4
// ReactiveSwift Merge Finish
μ°Έκ³
ReactiveX - Operators - Merge