This is the 7th day of my participation in the August Text Challenge.More challenges in August
preface
Over the past few articles, I’ve explained the following three types in RxSwift:
-
Observable (sequence), which can be used to generate asynchronous elements or a pipeline of synchronous elements.
-
An Observer is a type of sensor used to observe a sequence. It can perform three state processing on elements on the pipeline: Next, Error, and completed.
-
A Relay (both sequence and observer) is a Relay that is a combination of the two and can be used as a connector between sequence and observer.
The diagram in the previous section illustrates the power of the three:
In actual production, the assembly line is always doing the same thing: transforming elements into other elements as they are transported.
That’s what we’re going to look at in this section — sequence operators.
The operator
For example, in our production activities, the pork into ham sausage is an example:
- Pork is transported on the assembly line.
- The pork was transported to a processing plant.
- What comes out of the processing workshop is ham sausage.
The pseudo-code here is roughly:
Observable< pork > => Observable< ham >.
Let’s think about how to convert an array [01, 2, 3, 4, 5] to a string.
Of course you can use the for loop, but as a Swift programmer, it’s a bit of self-cultivation to use the higher-order functions that come with Swift:
let array = [0, 1, 2, 3, 4, 5]
let newArray = array.map { "\($0)" }
Copy the code
An array of type [Int] is then processed into an array of type [String].
Remember when I talked about observables, I compared them to arrays. Since arrays have a map function, does an Observable have a map function?
Some! And the usage is almost identical:
let observable = Observable.from([0, 1, 2, 3, 4, 5])
let newObservable= observable.map { "\($0)" }
Copy the code
At this point, we’re going to have oneObservable<Int>
The sequence of type is converted toObservable<String>
Type, and map is an operator in the sequence.
The function of the assembly line is not only to transfer elements, but also to shoulder the functions of pre-processing and processing. We should try our best to give the final form to the observer before finally giving it to the observer, so that the observer will be happy when using the elements.
The classic example of this is a network request, where we want to assign Model data to the UI, not JSON data, and we usually do a JSON-model conversion somewhere in the middle layer, and that JSON->Model change, which we do in RxSwift, is done over and over again.
Here is the code I wrote wanAndroid project, an interface network request:
Request (HomeService. QueryKeyword (keyword, page)) /// JSON < page <Info>> Map (BaseModel<Page<Info>>.self) /// get data from Model and form a new sequence. Map {$0. Data} // Filter out nil and form a new sequence. Single becomes a Maybe.compactMap {$0} /// converts to observable.asObservable () /// converts to Single.assingle ()Copy the code
This code uses 2 map and 1 compactMap, the purpose is to get a comfortable data, so that the ViewModel layer and View binding, smoother.
The ViewModel layer does the data processing, the View layer only does the data binding.
Matters needing attention
So far, we have covered only the simplest and most basic sequence operator in RxSwift, map, and sequences have as many as 57 operators (shown in the Chinese documentation).
I think, if I speak every time, with my faster writing speed, it may not be finished in a year.
In addition, there are so many operators that can only be used in special business scenarios. It is better to look for and try them when you need to use them.
For these operators, I have the following suggestions:
-
Understand the use of Swift’s own higher-order functions, their own higher-order functions understand, in fact, RxSwift these operators are not difficult.
-
Don’t be afraid of the 57 operator functions. They are like a general tool manual that you can flip through whenever you need to use some function or get some state.
-
The document’s decision tree may help you. If you don’t understand, go to the decision tree.
-
Learn about some common operators to compare:
- zip
- combineLatest
- flatMapLatest
conclusion
In this section I’ve explained the Observable operators.
The so-called assembly line is just like this. Raw materials are made into finished products step by step. During this period, coordination of multiple assembly lines may be needed.
The sequence of operations and more that we did above are all done by operators in the sequence.
Remember, a sequence is still a sequence after operator processing! It hasn’t come to an end yet, the observer in SUBSCRIBE is the last journey of an element.
Next video
In fact, I was going to write RxSwift to Rx thinking about the whole framework and industrial controls, but I accidentally wrote the sequence operator.
What to write behind, not clear, to be continued.
This is the end of my August reading campaign.
Reference documentation
Operator – The Operator
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: