CurrentValueSubject
μ λ€λ¦ ν΄λμ€ | νλμ κ°μ κ°μΈκ³ κ°μ΄ λ³νν λλ§λ€ μλ‘μ΄ μμλ₯Ό λ΄λ Subject
PassthroughSubject
μλ λ€λ₯΄κ² μ΄κΈ°κ°μ κ°μ§λ©°, κ°μ₯ μ΅κ·Όμ λ°νλ μμμ λν λ²νΌλ₯Ό μ μ§νλ€.
λ κ°μ μ λ€λ¦ νμ
μ κ°μ§λ€. νλλ κ°μ νμ
μ λνλ΄λ©°, λ€λ₯Έ νλλ μλ¬μ νμ
μ λνλΈλ€. μλ¬μ νμ
μ Error
νλ‘ν μ½μ μ±νν΄μΌ νλ€.
μ΄λμ λΌμ΄μ μ λ°νν μ΄κΈ°κ°μ λκ²¨μ£Όμ΄ μΈμ€ν΄μ€λ₯Ό μμ±νλ€.
value
νλ‘νΌν°λ₯Ό ν΅νμ¬ ν΄λΉ Subjectκ° κ°μΌ κ°μ μ κ·Όν μ μλ€.
let subject = CurrentValueSubject<Void, Never>(Void())
subject
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine CurrentValueSubject Error")
case .finished:
print("Combine CurrentValueSubject Finish")
}
}, receiveValue: {
print("Combine CurrentValueSubject")
})
.store(in: &cancellables)
subject.send(Void())
// Combine CurrentValueSubject
// Combine CurrentValueSubject
μ΄κΈ°κ°μ μ€μ νμμΌλ―λ‘ subject
λ₯Ό ꡬλ
ν μκ°μ κ°μ λ°μ κ²μ λν ν΄λ‘μ κ° μ€νλλ€.
μ΄ν subject.send(Void())
λ₯Ό νΈμΆνμ¬ Subjectμ κ°μ μ λ¬νμμΌλ―λ‘ κ°μ λ°μ κ²μ λν ν΄λ‘μ κ° ν λ² λ μ€νλλ€.
RxSwift
BehaviorSubject
λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
let subject = BehaviorSubject(value: Void())
subject
.subscribe(onNext: {
print("RxSwift CurrentValueSubject")
}, onError: { _ in
print("RxSwift CurrentValueSubject Error")
}, onCompleted: {
print("RxSwift CurrentValueSubject Finish")
})
.disposed(by: disposeBag)
subject.onNext(Void())
// RxSwift CurrentValueSubject
// RxSwift CurrentValueSubject
onNext(_:)
λ©μλλ₯Ό μ¬μ©νμ¬ Subjectμ κ°μ μ λ¬νλ€.
ReactiveSwift
MutableProperty
λ₯Ό μ¬μ©νκ³ producer
νλ‘νΌν°λ₯Ό ν΅ν΄ SignalProducerλ₯Ό λ§λ€μ΄ ꡬνν μ μλ€.
let property = MutableProperty(Void())
property.producer
.start { event in
switch event {
case .value:
print("ReactiveSwift CurrentValueSubject")
case .failed:
print("ReactiveSwift CurrentValueSubject Error")
case .completed:
print("ReactiveSwift CurrentValueSubject Finish")
default:
break
}
}
property.value = Void()
// ReactiveSwift CurrentValueSubject
// ReactiveSwift CurrentValueSubject
// ReactiveSwift CurrentValueSubject Finish
value
νλ‘νΌν°μ κ°μ ν λΉνμ¬ Propertyμ κ°μ μ λ¬νλ€.
MutableProperty
λ‘λΆν° signal
νλ‘νΌν°λ₯Ό ν΅ν΄ Signalμ λ§λ€μ΄ μ¬μ©νλ€λ©΄, MutableProperty
μ μ΄κΈ°κ°μ΄ νλ₯΄κ² λμ§ μμΌλ―λ‘ CurrentValueSubject
μ λμμ ꡬνν μ μλ€.
μ€μ½νλ₯Ό λ²μ΄λ λ property
κ° ν΄μ λμ΄ SignalProducerλ μ’
λ£νλ λͺ¨μ΅μ 보μ¬μ€λ€.
μ°Έκ³
Last updated
Was this helpful?