This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging

preface

In the previous section, we explained the Observable properties by comparing an Array to an Observable.

In this section, I’ll use real-life examples to illustrate Observable, tangible things that can be seen and felt, in the hope of enhancing our understanding of sequences.

Pipeline in reality

I don’t know if you find this time my first page of the article with pictures, and I used to style is very different, in the past my style of pictures, so is and programming related technical pictures, or to an ACG picture.

This time MY configuration is a production line scene, notice the line. The analogy here is an Observable and an pipeline.

Let’s first look at what the assembly line does:

Production line, also called assembly line, is an industrial mode of production in which each production unit focuses on only one piece of work to improve work efficiency and output.

Read more here.

The understanding of pipeline is very important, and the understanding of Rx will play a very important role.

The simplicity of the item

A simple assembly line, which is mainly used to transport goods, like the one we illustrated this time, is used to transport coke. In general, a production line is used to produce the same goods, because there is no need to distinguish between the types of goods.

Let’s look at the definition of an Observable:

public class Observable<Element> : ObservableType { . . . Code omission}Copy the code

An Observable has a generic constraint, which means that the elements in an Observable are all of the same type, similar to a pipeline that transmits a single item type.

Here if we wanted to define the pipeline of Coke as Observable, we could say it like this:

class CocaCola {}

let observable = Observable.just(CocaCola())
Copy the code

The difference between just and frome in Observable

In the Observables we used above, it would be more accurate to use the protocol just in ObservableType to build a sequence of single elements.

Let’s take a look at just.

createObservableEmit a unique element

The just operator converts an element into an Observable.

extension ObservableType {

    /// Returns an observable sequence that contains a single element.
    public static func just(_ element: Self.Element) -> RxSwift.Observable<Self.Element>

    /// Returns an observable sequence that contains a single element.
    public static func just(_ element: Self.Element, scheduler: RxSwift.ImmediateSchedulerType) -> RxSwift.Observable<Self.Element>

}
Copy the code

In contrast to from, which we used in the previous section, frome converts array-like data into a sequence:

extension ObservableType {
    /// Converts an array to an observable sequence.
    public static func from(_ array: [Self.Element], scheduler: RxSwift.ImmediateSchedulerType = CurrentThreadScheduler.instance) -> RxSwift.Observable<Self.Element>
    
    /// Converts a sequence to an observable sequence.
    public static func from<Sequence>(_ sequence: Sequence, scheduler: RxSwift.ImmediateSchedulerType = CurrentThreadScheduler.instance) -> RxSwift.Observable<Self.Element> where Sequence : Sequence, Self.Element == Sequence.Element

}

Copy the code

Observe events

If you can imagine that, if you can’t imagine that, you can look for videos of production lines.

The assembly line is constantly running, and people observe the operation of the assembly line through their eyes:

  • When things are put up, it is transported one by one.

    • You never know when something is moving on an assembly line and when it’s not. It’s a Future. It’s asynchronous.

    • Transportation on the assembly line is an asynchronous item.

  • When an error occurs in the operation of the assembly line, the general industrial control design will alarm through the buzzer.

    • The general assembly line alarm, the basic stop work.
  • When there is no goods transportation on the assembly line for a long time, the industrial control design will pass the inspection and think that there is no goods on the assembly line, and the assembly line will slow down or stop.

    • On this point, we can see by referring to the situation of the escalator, when there is no one, the escalator does not run or run slowly, when someone stands up, and resume normal operation.

    • In fact, the escalator can also be regarded as an assembly line.


With the pipeline out of the way, let’s look at Observable. The same code as before:

The sequence is generated by the from function, and the elements in the sequence are observed by subscribe.

let array = [0, 1, 2, 3, 4, 5] 

let observable = Observable.from(array) 

observable.subscribe { (event: Event<Int>) in

    switch event {

    case .next(let some):

        print(some)

    case .error(let error):

        print(error)

    case .completed:

        print(event.debugDescription)

    }

}.disposed(by: rx.disposeBag)

Copy the code

Event is an enumeration:

  • Next sees element after element emitted.

  • When an error occurs, an error is raised, and elements after the error are not emitted again.

  • Complete when all the elements in the sequence have been emitted.

Compare an assembly line to an Observable, and you’ll find that things are strikingly similar. The Event enumeration basically summarizes the events that occur for some typical characteristics of the pipeline.

The reason why it is called a typical feature event is because there are so many conditions that occur in the pipeline, but the code summarizes and summarizes the large event types.

public enum Event<Element> {

    /// Next element is produced.
    case next(Element)

    /// Sequence terminated with an error.
    case error(Swift.Error)

    /// Sequence completed successfully.
    case completed
}

Copy the code

I even think that the birth and evolution of Rx is not accidental, but a product of human production labor through code summary, because compared to programming, the production line has been around for too long.

In 1769, Josiah Wedgwood, an Englishman, opened a ceramic factory in Eturia, where he practiced a fine division of labor. He divided the process of making pottery, which was originally completed by one person from beginning to end, into dozens of specialized processes, which were completed by special people. In this way, the original sense of “potter” no longer exists, there is only the existence of clay digging, clay transport, earth decoration, billet workers and other potter artisans become workers in the pottery factory, they must work according to a fixed pace of work, subject to unified labor management.

(According to the above data, it is clear that Wedgwood’s working method can be fully defined as “assembly line”. The other is that Henry Ford invented the assembly line, which is obviously inaccurate, since Henry Ford was born in 1863, more than 90 years after Wedgwood lived.)

A driveway

If the assembly line is still not familiar to you, please recall the escalator, or a lane, and you are the person standing at the side of the lane counting cars:

  • You never know when the car will come, it may be in the near future, it may be waiting for a long time, there will not be a car, so you can only quietly observe, a car comes, count one.

  • The car is an asynchronous element.

  • A lane is not easy to understand.

conclusion

  • In the last section, we used the programming language analogy of Array and Observable to understand sequences.

  • In this section, we interpret sequences in a materialized way by comparing a pipeline with an Observable in reality.

  • The logical thinking of industrial programming can be used to understand Rx.

  • In some programming languages, it makes sense to use Stream to represent sequences. Examples include Dart’s asynchronous programming framework and Swift’s Combine framework.

We’ll see that in the next video

Whether it is industrial control or programming, we are always faced with the problem of selection. Observable is the most common sequence, and Rx also provides us with some special sequences for selection.

We’ll look at them in the next video.

Reference documentation

RxSwift Chinese document /just

RxSwift Chinese document /from

RxSwift writes wanAndroid client now open source

RxSwift is now open-source to write wanAndroid clientsProject link.

I took a round of CodeReview and added a few small features:

  • Added dark Mode adaptation for iOS

  • Pull-up loads use more of the no-perception feature

  • MBProgressHUD is replaced by SVProgressHUD

Remember to give a star oh!

Attach an image of the effect: