This is the sixth day of my participation in the August Text Challenge.More challenges in August
The statement
For official documentation consistency, I’ll stick to the long, smelly name Observable & Observer, which is both a listening sequence and an Observer.
But I want to say that the type of component I’m going to talk about today is usefulRelay
This concept is enough to illustrate!
In addition, I do not understand the frequent use of the word Subject in RxSwift, but I do not understand its meaning, and hope that the big guys give advice.
preface
In the last section, I explained the Observer, and explained that the sequence is like an assembly line of industrial operations, used for output.
I liken the observer to an industrial sensor that receives signals from a sequence, for input.
In fact, in the whole industrial control system, some sensors not only receive signals, but also need to convert the signal and output, this component is also called a relay.
Understanding Relay
While studying the Observable&Observer type, I looked at the BehaviorRelay and PublishRelay classes, which are a relatively independent module in a folder called RxRelay.
BehaviorRelay and PublishRelay correspond to two types of BehaviorSubject and PublishSubject respectively. Their internal implementations are encapsulated by one layer of BehaviorSubject and PublishSubject. The specific code is not expanded:
public final class BehaviorRelay<Element>: ObservableType {
private let _subject: BehaviorSubject<Element>
/// Accepts `event` and emits it to subscribers
public func accept(_ event: Element) {
self._subject.onNext(event)
}
.
.
.
Copy the code
public final class PublishRelay<Element>: ObservableType { private let _subject: PublishSubject<Element> // Accepts `event` and emits it to subscribers public func accept(_ event: Element) {self._subject.onnext (event)} · · ·Copy the code
What really interests me is this word Relay:
Relay (English name: relay) is an electrical control device, is when the input (excitation) of the change to meet the specified requirements, in the electrical output circuit so that the controlled quantity has a predetermined step change of an electrical appliance. It has the interaction between the control system (also known as the input loop) and the controlled system (also known as the output loop). Usually used in automatic control circuits, it is actually a small current to control the operation of a large current “automatic switch”. Therefore, it plays the role of automatic adjustment, safety protection and conversion circuit in the circuit.
Note that in this reference to Relay, the output loop and input loop are very similar to observables and observers, as shown below from RxSwift:
That’s why I call the type of relay in RxSwift that has this property.
It is from Relay that I think of industrial control (really thanks to the years of experience in industrial control programming), and then look at RxSwift from the perspective of industrial control, the whole world suddenly opened up.
A component that can serve as both input and output is called Observable & Observer in RxSwift.
The use of Relay
In RxSwift, a Relay can either generate a sequence or consume it as an observer.
- Can produce and sell themselves:
Let subject = PublishSubject<Int>() Event<Int>) in switch event { case .next(let some): print(some) case .error(let error): print(error) case .completed: print(event.debugDescription) } }.disposed(by: OnNext (0) subject.onnext (1) subject.onnext (2) subject.onnext (3) subject.onnext (4) subject.onNext(5)Copy the code
The following logs are displayed:
0
1
2
3
4
5
completed
Copy the code
- You can find a sequence, and then Relay to connect to the processing:
Let Observable = Observable. From ([0, 1, 2, 3, 4, 1]) let subject = PublishSubject<Int>() Subscribe {(event: event <Int>) in switch event {case.next (let some): Some + some print(some + some) case. error(let error): print(error) case. completed: Prompt (event.debugDescription)}}. Disposed (by: disposeBag) //Copy the code
The following logs are displayed:
0
2
4
6
8
10
completed
Copy the code
Matters needing attention
Whether the Relay sells itself or binds to the Relay via other Observables, we subscribe first, then use onNext to generate the sequence or bind(to:), which is entirely determined by the characteristics of the Relay itself:
The PublishSubject will send elements generated after the subscription to the observer, and elements issued before the subscription will not be sent to the observer.
Through the selection of relay, to achieve different effects. In fact, it is consistent with the selection of relay in the design of industrial control.
AsyncSubject, PublishSubject, ReplaySubject, BehaviorSubject, ControlProperty
As in the previous section, there are two types of Observers: AnyObserver and Binder.
There are five common types of Relay: AsyncSubject, PublishSubject, ReplaySubject, BehaviorSubject, and ControlProperty.
Here I briefly extract the differences between these Relay agents in Chinese documents.
The AsyncSubject emits the last element (and only the last element) after the source Observable emits a completion event, or only a completion event if the source Observable emits no elements.
The PublishSubject will send elements generated after the subscription to the observer, and elements issued before the subscription will not be sent to the observer. If you want the observer to receive all elements, you can create an Observable by using the Create method of an Observable, or by using ReplaySubject.
The ReplaySubject will send all elements to the observer, regardless of when the observer subscribed.
When the observer subscribles to the BehaviorSubject, it emits the latest element in the source Observable (default element if none exists). The subsequent elements are then sent out.
ControlProperty is specifically used to describe UI control properties.
Matters needing attention
Why don’t I go through all the Relay types? The main reasons are as follows:
-
PublishSubject I have given examples, the rest are just examples.
-
The Chinese documentation has very detailed explanations and code examples.
-
Paper come zhongjue shallow, self-study is the right way!
-
Understand BehaviorSubject and PublishSubject, and BehaviorRelay and PublishRelay are compatible.
conclusion
In this section, we introduce a type of RxSwift that is both a listening sequence and an observer. Here we call Relay. I explain the use of this type from the Angle of industrial control Relay.
Write the code so that you can understand this special type that can be sold by itself, bound to other sequences and output.
As for the types of Relay, I simply copied the information in the document. My personal advice is to understand one of them first and then break them down one by one, which is more profound than I can explain.
Next video
RxSwift learning this series, in fact, I do not know how many pieces to write, but the current length: sequence, observer, relay three important core types have been explained.
I personally think these three core types are understood, the rest are just actual coding and addenda.
Of course, if you like my way of understanding from the perspective of industrial control, it does not hinder to continue, mainly I think few people like, 😂
In the next article, I’m going to start with RxSwift and look at Rx from a broader perspective.
Reference documentation
Observable & Observer is both a listening sequence and an Observer
The WanAndroid client written by RxSwift is now open source
Currently RxSwift written wanAndroid client has been open source –Project link.
I took a round of CodeReview and added a few small features:
-
Added dark Mode adaptation for iOS
-
Pull-up loading uses more imperceptive features
-
Replace MBProgressHUD with SVProgressHUD
Remember to give a star!
Attached is an effect picture: