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