Swift Learning: Responsive Programming RxSwift

reactive programming

  • Reactive Programming (RP)
  1. It is also a programming paradigm, proposed in 1997, which can simplify asynchronous programming and provide more elegant data binding 
  2. 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
  1. RAC for short, with Objective-C and Swift versions
  2. Official website: http://reactivecocoa.io/
  3. github:https://github.com/ReactiveCocoa
  • ReactiveX
  1. Rx for short, there are many programming language versions, such as RxJava, RxKotlin, RxJS, RxCpp, RxPHP, RxGo, RxSwift, etc. 
  2. Official website: http://reactivex.io/
  3. github: https://github.com/ReactiveX

RxSwift

  • RxSwift (ReactiveX for Swift), the Swift version of ReactiveX
  1. Source code: https://github.com/ReactiveX/RxSwift
  2. 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
  1. next: carry specific data
  2. error: carries an error message, indicating that the Observable is terminated and no more events will be emitted 
  3. completed: Indicates that the Observable has terminated

The underlying code of Event:

  • dispatcher Scheduler
  1. MainSchedulerThe main thread, all UI-related tasks are executed under this thread
  2. SerialDispatchQueueSchedulerEquivalent to GCDthe corresponding serial queue
  3. ConcurrentDispatchQueueSchedulerEquivalent to GCDa parallel queue
  4. OperationQueueSchedulerEquivalent to NSOperationQueuethe manager can set the number of concurrency
  5. CurrentThreadScheduler- 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
  1. KVO
  2. Target-Action (...addTarget...)
  3. Notification
  4. 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.

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_42433480/article/details/100065750