reactive programming
- Reactive Programming (RP)
- It is also a programming paradigm, proposed in 1997, which can simplify asynchronous programming and provide more elegant data binding
- Generally, it is integrated with functional style, so it is also called: Functional Reactive Programming (FRP for short)
- Well-known and mature responsive framework
- ReactiveCocoa
- RAC for short, with Objective-C and Swift versions
- Official website: http://reactivecocoa.io/
- github:https://github.com/ReactiveCocoa
- ReactiveX
- Rx for short, there are many programming language versions, such as RxJava, RxKotlin, RxJS, RxCpp, RxPHP, RxGo, RxSwift, etc.
- Official website: http://reactivex.io/
- github: https://github.com/ReactiveX
RxSwift
- RxSwift (ReactiveX for Swift), the Swift version of ReactiveX
- Source code: https://github.com/ReactiveX/RxSwift
- Chinese Documentation: https://beeth0ven.github.io/RxSwift-Chinese-Documentation/
- There are already detailed installation tutorials on the github of RxSwift, here only demonstrates the installation of CocoaPods
- RxSwift: Swift implementation of the Rx standard API, excluding any iOS-related content
- RxCocoa: Based on RxSwift, it extends many Rx features to iOS UI controls
The core role of RxSwift
- Observable: responsible for sending events (Event)
- Observer: Responsible for subscribing to Observable and listening to events sent by Observable (Event)
- Illustration:
- There are 3 types of events
- next: carry specific data
- error: carries an error message, indicating that the Observable is terminated and no more events will be emitted
- completed: Indicates that the Observable has terminated
The underlying code of Event:
- dispatcher
Scheduler
MainScheduler
The main thread, all UI-related tasks are executed under this threadSerialDispatchQueueScheduler
Equivalent toGCD
the corresponding serial queueConcurrentDispatchQueueScheduler
Equivalent toGCD
a parallel queueOperationQueueScheduler
Equivalent toNSOperationQueue
the manager can set the number of concurrencyCurrentThreadScheduler
- the current thread
- destroyer
Dispose
Create and subscribe to Observable (1)
Observable creation:
Observables are subscribed using subscribe:
Disposable
- Whenever an Observable is subscribed, a Disposable instance will be returned. When Disposable's dispose is called, it is equivalent to unsubscribing
- When you no longer need to receive events, it is recommended to unsubscribe and release resources. There are 3 common ways to unsubscribe
Create and subscribe to Observable (2)
Timer creates Observable:
Create Observers
Extended Binder properties
Traditional status monitoring
- In development, it is often necessary to monitor various states. The traditional common monitoring solutions are
- KVO
- Target-Action (...addTarget...)
- Notification
- DelegatepBlock Callback
- Traditional solutions often have intricate dependencies, high coupling, and the need to write repetitive non-business code
RxSwift status monitoring
Listening for button clicks:
button.rx.controlEvent(.touchUpInside).subscribe(onNext: {
print("按钮被点击了")
}).disposed(by:bag)
The following method achieves the same effect as above:
Attribute monitoring:
Both Observable and Observer
- Attribute values such as UISlider.rx.value and UTextField.rx.text are both Observable and Observer
- They are of type RxCocoa.ControlProperty
Application of tableView in RxSwift
Note: If the delegate of the tableView is set, the rx method of the above tableview set by RxSwift will be invalid.