RxSwift’s core process from the source code analysis principle, divided into three steps

1. Create sequence — create 2. Subscribe 3. Send the signal — onXX

//1. Create sequencelet ob = Observable<Any>.create { (observer) -> Disposable in//3. Signal observer.onnext ("Send a signal")
            observer.onError(NSError.init(domain: "EpisodeError", code: 10086, userInfo: nil))
            returnDisposables. Create ()} //2let _ = ob.subscribe(onNext: { (text) in
            print("Subscribe to :\(text)")
        }, onError: { (error) in
            print("error:\(error)")
        }, onCompleted: {
            print("Complete") {})print("Destroyed")}Copy the code

So the question is, how do the signals we send get subscribed to?

From code analysis we can see that 1. We created closures 1 and 2 when we sent the signal. When we subscribe, we subscribe closure 2–onXX 3. Closure 2’s onXX will echo closure 1’s onXX, passing in the value of closure 1

How is the bottom of the implementation of these three steps, so we analyze one by one from the source code

1. Create sequence — create

– Create method creates an internal object AnonymousObservable -AnonymousObservable holds the external subscribeHandler closure

2. Subscribe sequence — SUBSCRIBE The subscribe sequence source in the extension of ObservableType

extension ObservableType {
    public func subscribe(onNext: ((Element) -> Void)? = nil, onError: ((Swift.Error) -> Void)? = nil, onCompleted: (() -> Void)? = nil, onDisposed: (() -> Void)? = nil)
        -> Disposable {
            ...
            
            let observer = AnonymousObserver<Element> { event in
                switch event {
                case .next(letvalue): onNext? (value)case .error(let error):
                    if let onError = onError {
                        onError(error)
                    }
                    else {
                        Hooks.defaultErrorHandler(callStack, error)
                    }
                    disposable.dispose()
                case .completed:
                    onCompleted?()
                    disposable.dispose()
                }
            }
            return Disposables.create(
                self.asObservable().subscribe(observer),
                disposable
            )
    }
}
Copy the code

-Element is the association type for Swift, and this Element here is our sequence type – creates an AnonymousObserver, whose initialization is the closure parameter, Now save the external onNext, onError, onCompleted, and onDisposed calls to the callback closure

There is also a very important sink that plays the role of dispatching

3. Send signals – the source code for onXX to send signals exists in the ObserverType extension

Summary: RxSwift structure

1: Sequence – coding unity, anytime, anywhere 2: Sinking of some columns of requirements through functional thinking (encapsulating things that developers don’t care about) – optimizing code, saving logic 3: The most typical feature of RxSwift is to solve the response capability of Swift, a static language, using the time-dimensional series as the axis, the user subscription care can be kept alive along with the axis, subscription once, the response has been continued ~