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(術(shù)語表)
- 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: 發(fā)生該訂閱事件時拋出,攜帶最新的數(shù)據(jù)
? A completed event: 成功完成了該訂閱事件是拋出
? An error event: 錯誤發(fā)生時會拋出
Operators
ObservableType
和Observable
類包含了大量異步功能的離散的抽象方法 分唾,我們可以任意組合這些方法來實現(xiàn)更復雜的邏輯,將這些高度獨立和可組合化的方法塊斗塘,稱之為Rx的Operators恬惯。
例如:實時監(jiān)聽設備方向,然后執(zhí)行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的簡化封裝,更易于鏈式調(diào)用和使用奕锌。
- SerialDispatchQueueScheduler串行隊列
- ConcurrentDispatchQueueScheduler并行隊列
App architecture
RxSwift只會負責處理事件著觉、異步的數(shù)據(jù)序列和全局的通信合約,所以它不會更改你的項目框架惊暴,所以你可以選擇MVC饼丘、MVP或MVVM都是可以的。
這種情況下缴守,你是沒必要完全采用Rx來開發(fā)一個新的工程葬毫,而是可以重構(gòu)一些模塊用Rx,或?qū)⑿略龅墓δ苡肦x來開發(fā)屡穗。
不過贴捡,值得注意的是微軟的MVVM模式是特別適合Rx這種數(shù)據(jù)驅(qū)動的開發(fā)框架的,而且ViewModel也可以很方便地暴露Observable<T>的屬性給View村砂,讓UIKit空間和數(shù)據(jù)綁定變得容易簡單烂斋。
RxCocoa
- RxSwift是Rx基于Foundation對Swift的基本數(shù)據(jù)類型和操作方法進行的擴展庫
- 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
官方社區(qū):http://community.rxswift.org
詳細安裝可以見我簡書另一篇文章《RxSwift | 從一個簡單的UITableView入手》
RxSwiftCommunity也有一些第三方擴展庫如RxRealm、RxAlamofire、RxDataSources汛骂、RxKeyboard罕模、RxSwiftExt、RxAnimated帘瞭、RxTheme淑掌、RxTest、RxBlocks等
這一章節(jié)到這里就結(jié)束了蝶念,原作出自Raywenderlich
如果對你有幫助抛腕,別忘了點個贊和關注~