RxSwift(iii) Explore the creation sequence of RxSwift core logic

RxSwift(4) Explore the subscription sequence of RxSwift core logic

RxSwift(5) Explore the RxSwift core logic of sending signals and summary

preface

We’ve seen the benefits of RxSwift, but how does it implement the logic of creating sequences, subscribing to sequences, and sending signals? This article is the author’s own way to explore, for everyone to learn and reference.

RxSwift process analysis

Let’s take a look at the common steps to use RxSwift: 1. Create sequence 2. Subscribe sequence 3. Send signals

Observable<String>. Create {(observer) -> Disposablein// 3: send signal observer.onnext ("This is the signal.")
        return// 2: Subscribe}. Subscribe (onNext: {(text)in
            print("Subscribe to :\(text)")})Copy the code

So let’s just think about it, this is a signal how did the word subscribe get printed in the closure of subscribe? Actually according to the response type is RxSwift function programming framework that, we can bold speculation here is through the nested closure to execute this response code, at the same time there must be a logic similar to the flow of events inside the pipeline, because it can be continuously sending response, then subscribe to the closure of can also received a steady stream of each response.

Sequence creation explores the process

Let’s start with the first step of the process. How does RxSwift create the sequence and what does it do in the process?

  • First of all, let’s go into the sequence creation method and see what we’re creating

  • I can see the create method, but when I click on the create implementation like above, I can’t click on it, so I need to look at the comment, and it says I can see the create implementation on another route, so I do a global search

  • You can obviously see the implementation of the Create method in create. swift (you can make a breakpoint, and you can see that the breakpoint will come in here), and you can see that it will return the AnonymousObservable(AnonymousObservable sequence) type. Click on the implementation again

  • The AnonymousObservable has two methods :init, which holds the subscribe closure, and run, which guesses that it might be associated with executing the closure, but we won’t explore that for now. We find that AnonymousObservable inherits from Producer, which is literally related to the creation of sequences. We click on Producer

  • We see the same run method here, we see a subscribe method, we don’t see anything associated with creating a sequence right now, so let’s go to its parent Observable

  • The composeMap method seems useless (as defined by its annotations). You can see that it also has a parent class ObservableType

  • In ObservableType, can see only asObservable implementation and the subscribe method statement, you can see there are parent ObservableConvertibleType, then click on view

public protocol ObservableType : ObservableConvertibleType {
    func subscribe<O: ObserverType>(_ observer: O) -> Disposable where O.E == E
}
extension ObservableType {
    /// Default implementation of converting `ObservableType` to `Observable`.
    public func asObservable() -> Observable<E> {
        // temporary workaround
        //return Observable.create(subscribe: self.subscribe)
        return Observable.create { o in
            return self.subscribe(o)
        }
    }
}
Copy the code
  • In ObservableConvertibleType, can see associated E, also is the Element in front of us, also declares the asObservable method, explore here is the end point
public protocol ObservableConvertibleType {
    /// Type of elements in sequence.
    associatedtype E

    /// Converts `self` to `Observable` sequence.
    ///
    /// - returns: Observable sequence that represents `self`.
    func asObservable() -> Observable<E>
}
Copy the code

Inheritance chain

Sort out the contents explored all the way and summarize a simple inheritance chain. This inheritance chain is relatively complex and more nested. Here we need to master what:

  1. The internal object created by the create method is of type AnonymousObservable
  2. The AnonymousObservable init method holds the subscribeHandler that is passed in externally
  3. AnonymousObservable inherits the very important run() method from Producer
  4. The asObservable() in Observable creates the magic effect of everything sequence

conclusion

So this is my whole exploration of creating a sequence, so if you’re interested in exploring it yourself,