Make writing a habit together! This is the 15th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.
- Let’s move on to the rest
subject
Simple analysis of the use and principle of type
1. ReplaySubject
In contrast to the BehaviorSubject we analyzed earlier, which saved a default value, our ReplaySubject could set the elements we wanted to save. Look at an example
/// Customize the size of the array to store values
func ReplaySubjectMethod() {
let ReplaySubject = ReplaySubject<Int>.create(bufferSize: 3)
ReplaySubject.onNext(4)
ReplaySubject.onNext(5)
ReplaySubject.onNext(6)
ReplaySubject.subscribe(onNext: {print("ReplaySubject first Subscription", $0)})
.disposed(by: disposeBag)
ReplaySubject.onNext(7)
ReplaySubject.subscribe(onNext: {print("ReplaySubject subscribe again", $0)})
.disposed(by: disposeBag)
}
Copy the code
Print the result
You can see that the last three elements are saved, because when we initialize thembufferSize
Set the size to 3. Similar to the size of our array
- Analysis of the
Initialization Determines whether bufferSize is 1
See ReplayOne first
It saves the last element that we sent,replayBuffer
Is called by the observerOn methods
send
ReplayMany
This is inherited fromReplayManyBase
Attributes,queue
Is an array forstore
The element sent by our observer.
- Send the event
When we send the next event, we add our element to the array when we call on
- To subscribe to
ReplayBuffer () is called, which we overwrote above in ReplayManyBase and ReplayOne, to send the saved Element to the current subscriber. Also add our observer callback event to obsevers.
2.AsyncSubject
AsyncSubject again
/// Send the last value after completing the sequence
func AsyncSubjectMethod() {
let asyncSubject = AsyncSubject<Int>.init()
asyncSubject.onNext(4)
asyncSubject.onNext(5)
asyncSubject.onNext(6)
asyncSubject.subscribe(onNext: {print("AsyncSubject first subscription", $0)})
.disposed(by: disposeBag)
asyncSubject.onNext(7)
asyncSubject.subscribe(onNext: {print("AsyncSubject subscribes again", $0)})
.disposed(by: disposeBag)
asyncSubject.onCompleted()
}
Copy the code
You can see an isStopped state, a stoppedEvent event, and a set method defined. The lastElement is defined.
- On
When an event is sent
When the.next event saves the element and initializes an empty eventHandle array. When it completes, it determines whether the last element exists, assigns stoppedEvent, and sends the next event. Otherwise send a complete event
Determine the event based on the event. We only get to the next event if we send the complete event.
Call the event callback at Dispatch, where our next event is called first, followed by the complete event.
- summary
For the AsyncSubject class, our event is not called when we send the onNext event. Only when we send on Completed event is the last saved element sent to all subscribers’ event callbacks, ending the sequence.
3. BehaviorRelay
For the elements sent by our sequence, we can only get them every time we subscribe. Can we actively get them?
func BehaviorRelayMethod() {
let behaviorRelay = BehaviorRelay<Int>(value: 2)
print(behaviorRelay.value)
behaviorRelay.subscribe(onNext: {print($0)})
.disposed(by: disposeBag)
behaviorRelay.accept(20)}Copy the code
Our BehaviorSubject, for example, cannot print results of error and complete events
- Analysis of the
A BehaviorSubject encapsulates a BehaviorSubject, which cannot send an error or complete event, but also gets a value sent
Use Accept instead of our onNext(event) when sending
4. To summarize
For our Subject class, it can both send events and subscribe, greatly facilitating our development. Choose according to our actual development needs.