1. Introduction to Rx

1. What is Rx?

  • Rx, short for ReactiveX, is simply reactive programming based on asynchronous events (events) sequences.

  • Rx can simplify asynchronous programming methods and provide more elegant data binding. It allows us to respond to new data all the time and process it sequentially.

2, the Rx library

  • Rx itself is a cross-platform standard, with its own community forum that allows you to get your work done on both the Web and mobile using Rx’s thinking and approach.

  • As a cross-platform standard, there are many libraries for Rx based on different development languages. In addition to RxSwift, which I’ll focus on later, there are RxJava, RxJS, RxKotlin, Rx.NET… And so on.

  • Although these Rx libraries are in different languages, they are actually interlinked and share the same API. So if you do other aspects of development in another language, you can still use the same thinking and even the same method interface (except in different languages) to program.

3. Rx homepage

  • To learn more about the entire Rx family, visit its home page: reactivex.io
  • Here we can see all the supported languages, as well as the platforms:

Ii. Introduction to RxSwift

1. The role of RxSwift

(1) When writing codes, we often need to detect changes of some values (such as textFiled input value changes, data request completion or failure changes) and then process them accordingly.

  • In the past, different event-passing methods were used for different situations, such as Delegate, notification, target-Action, KVO, etc.

  • The appearance of RectiveX mechanism (implemented by RxSwift) unify the event passing response method in the program. Replace the usual event-passing methods (delegate, notification, target-Action, etc.) with Rx’s “signal chain” approach.

(2) If we usually use MVVM development mode, through RxSwift can obtain more convenient data binding method, making MVVM development more powerful.

2. Installation and configuration of RxSwift

(1) Download the latest code from GitHub: github.com/ReactiveX/R…

(2) Drag and drop rx. xcodeProj from the downloaded source package into your project

(3) Project -> General -> Embedded Binaries to add iOS version rxSwift. framework, rxCocoa. framework

(4) Finally, import RxSwift where it is needed

import RxSwift
import RxCocoa
Copy the code

3, RxSwift and RxCocoa

In the previous configuration, we will introduce the RxSwift and RxCocoa libraries in the Swift project. They do the following:

  • RxSwift: It is based only on the Rx standard implementation library of the Swift language, so RxSwift does not contain any Cocoa or UI classes.

  • RxCocoa: A library developed for iOS based on RxSwift that adds Rx features to native UI controls via Extension, making it easier to subscribe to and respond to their events.

The original source: www.hangge.com reproduced please keep the original link: www.hangge.com/blog/cache/…