01. RxSwift (1) ———— Basic Concepts

It will take about 3 minutes to finish reading this article.

1. RxSwift and ReactiveCocoa

RxSwift is a functional responsive programming language developed by Rx for the Swift language, like the ReactiveCocoa of Objective-C. It can replace the existing Target Action/proxy/closure/notification/KVO, but also provides networking, data binding, UI event handling, UI display and update, multi-threading, and so on.
It has the following advantages:

(1) Swift is a value type, which has an impact on value transfer and method callbacks. RxSwift makes up for the flexibility of Swift to a certain extent.

(2) RxSwift makes code reusable and reduces the amount of code.

(3) RxSwift increases code readability because declarations are immutable.

(4) RxSwift makes it easier to understand business code, abstract asynchronous programming, and unify code style.

(5) RxSwift makes it easier to write integrated unit tests and increases code stability.

1.1 RxSwift is not a simple Swift version of RAC

RAC and Rx can be said to be two completely different species. RAC believes that it is very necessary to distinguish the observed objects into hot/cold, and this is also a core feature of their framework. Rx is developed strictly in accordance with the regulations of the ReactiveX organization.

1.2 RAC’s hot and cold signals

RAC3.0 mainly has two entities, signal and SignalProducer. Signal can publish events regardless of whether subscribers are bound. SignalProducer will only be triggered when a signal or event occurs. These two distinctions are made to distinguish cold signals from hot signals.

In RxSwift, all objects are observables. signal and SignalProducer become Observable, these two entities are the same thing in Rx. Creating Observables in RxSwift does not need to consider whether it is a cold signal or a hot signal.

2. Basic concepts

2.1 Events and Sequence

RxSwift regards every operation in our program as an event, such as the text in a TextField changes, a button is clicked, or the end of a network request, etc. Each event source can be regarded as a pipeline or sequence, that is, sequence , such as TextField, when we change the text inside, this TextField will continue to emit events and continuously flow out from its sequence. We only need to monitor this sequence and handle each event accordingly. In the same way, Button is also a sequence, and an event flows out every time it is clicked. That is to say, we can understand RxSwift by thinking of each step as an event. As shown below:

Observable is observable, and it is also an event source. Observers can receive event notification messages from Observable by subscribing.

Creating an observable is actually creating an Obserable sequence, which is creating a stream, which can then be subscribed, and we can perform corresponding processing in the corresponding subscriber code.

2.2 Observable observer pattern

Observable is observable, and it is also an event source. Observers can receive event notification messages from Observable by subscribing.

Creating an observable is actually creating an Obserable sequence, which is creating a stream, which can then be subscribed, and we can perform corresponding processing in the corresponding subscriber code.

2.3. Source code address

https://github.com/ReactiveX/RxSwift
Introduced in Podfile or Podspec:

1
2
pod 'RxSwift', '6.6.0'
pod 'RxCocoa', '6.6.0'

Add the following to the corresponding code file:

1
2
import RxSwift
import RxCocoa