RxSwift: Variables
- Wraps a
BehaviorSubjectand stores its current state. - Can access the current value via
valueproperty. - Use
valueto set a new element onto a variable (Don’t useonNext(_:)). - Because it wraps a
BehaviorSubjectit is create with an initial value and it will replay its initial value to new subscribers. - To access a variables
BehaviorSubjectcallasObservable(). - Guaranteed NOT to emit an error.
- Can listen for error events but cannot add one.
- Will also automatically
completewhen it is about to be deallocated so you do not and cannot manually add.completed. - Like any other subject it can be subscribed to by an observable to be reacted to when a new
.nextevent is emitted. - Useful for one off needs such as checking the current value without subscribing to receive updates.
var variable = Variable("James Forrest")
variable.value = "Scott Brown"
< All Posts