RxSwift is a library for composing asynchronous and event-based code by using observable sequences and functional style operators, allowing for parameterized execution via schedulers.
RxSwift, in its essence, simplifies developing asynchronous programs by allowing your code to react to new data and process it in sequential, isolated manner.
Introduction to asynchronous programming
Cocoa and UIKit Asynchronous APIs
- NotificationCenter
- The delegate pattern
- Grand Central Dispatch
- Closures
Asynchronous programming glossary(术语表)
- State, and specifically, shared mutable state
- Imperative programming
- Side effects
- Declarative code
RxSwift combines some of the best aspects of imperative code and functional code.
- Reactive systems
- Responsive
- Resilient
- Elastic
- Message driven
Foundation of RxSwift
A team at Microsoft worked on a library, independently from the core teams in the company, and sometime around 2009, offered a new client and server side framework called Reactive Extensions for .NET (Rx).
It was an installable add-on for .NET 3.5, and later became a built-in core library in .NET 4.0. It’s been an open source component since 2012.
Open sourcing the code permitted other languages and platforms to reimplement the same functionality, which turned Rx into a cross-platform standard.
RxSwift finds the sweet spot between traditionally imperative Cocoa code and purist functional code. It allows you to react to events by using immutable code definitions to process asynchronously pieces of input in a deterministic, composable way.
The three building blocks of Rx code are observables
, operators
, and schedulers
.
Observables
class Observable
public class Observable<Element> : ObservableType{
}
protocol ObservableType
// 基础方法subscribe
public protocol ObservableType : ObservableConvertibleType{
func subscribe<O: ObserverType>(_ observer: O) -> Disposable where O.E == E
}
// 基础扩展方法`asObservable()`
extension ObservableType {
public func asObservable() -> Observable<E> {
}
}
一个Obvervable对象只能抛出3个事件:
• A next event: 发生该订阅事件时抛出,携带最新的数据
• A completed event: 成功完成了该订阅事件是抛出
• An error event: 错误发生时会抛出
Operators
ObservableType
和Observable
类包含了大量异步功能的离散的抽象方法 ,我们可以任意组合这些方法来实现更复杂的逻辑,将这些高度独立和可组合化的方法块,称之为Rx的Operators。
例如:实时监听设备方向,然后执行filter、map等Operators,然后subscribe给onNext响应,完成整个事件序列。
UIDevice.rx.orientation
.filter { value in
return value != .landscape
}
.map { _ in
return "Portrait is the best!"
}
.subscribe(onNext: { string in
showAlert(text: string)
})
Schedulers
Schedulers相当于简化版的GCD,是Rx对GCD的简化封装,更易于链式调用和使用。
- SerialDispatchQueueScheduler串行队列
- ConcurrentDispatchQueueScheduler并行队列
App architecture
RxSwift只会负责处理事件、异步的数据序列和全局的通信合约,所以它不会更改你的项目框架,所以你可以选择MVC、MVP或MVVM都是可以的。
这种情况下,你是没必要完全采用Rx来开发一个新的工程,而是可以重构一些模块用Rx,或将新增的功能用Rx来开发。
不过,值得注意的是微软的MVVM模式是特别适合Rx这种数据驱动的开发框架的,而且ViewModel也可以很方便地暴露Observable<T>的属性给View,让UIKit空间和数据绑定变得容易简单。
RxCocoa
- RxSwift是Rx基于Foundation对Swift的基本数据类型和操作方法进行的扩展库
- RxCocoa则是对UIKit中的控件属性进行的Rx扩展库
例如UISwitch:
toggleSwitch.rx.isOn
.subscribe(onNext: { enabled in
print( enabled ? "it's ON" : "it's OFF" )
})
Installing RxSwift
RxSwift是Github上开源的一个免费框架:https://github.com/ReactiveX/RxSwift
中文文档:https://beeth0ven.github.io/RxSwift-Chinese-Documentation
详细安装可以见我简书另一篇文章《RxSwift | 从一个简单的UITableView入手》
RxSwiftCommunity也有一些第三方扩展库如RxRealm、RxAlamofire、RxDataSources、RxKeyboard、RxSwiftExt、RxAnimated、RxTheme、RxTest、RxBlocks等
这一章节到这里就结束了,原作出自Raywenderlich
如果对你有帮助,别忘了点个赞和关注~