Observable 是在 Swift 中观察值的最简单方法
Observable 是在 Swift 中观察值的最简单方法。
如何
创建可观察和可变可观察
使用您可以创建和观察事件。
使用您可以观察事件,以避免对我们内部 API 的副作用。MutableObservable
Observable
使用自定义释放功能创建观察器
在某些情况下,可观察量在处于活动状态时需要资源,在处置时必须清理这些资源。要处理此类情况,您可以将可选块传递给可观察初始值设定项,以便在处置可观察量时执行。
模型属性作为@MutableObservable
现在将绑定/映射的属性标记为可观察并导出公共可观察
Add an observer
Add an observer and specify the DispatchQueue
Change the value
Stop observing new values
Memory management
For a single observer you can store the returned to a variableDisposable
For multiple observers you can add the disposable to a variableDisposal
And always weakify when referencing inside your observerself
self
Installation
CocoaPods
Observable is available through CocoaPods. To install
it, simply add the following line to your Podfile:
Swift Package Manager
Observable is available through .
Swift Package Manager (SwiftPM) is a tool for automating the distribution of Swift code.
It is integrated into the swift compiler and from Xcode 11, SwiftPM got natively integrated with Xcode.Swift Package Manager
Migrations
1.x.y to 2.0.0
Observable
is nowMutableObservable
ImmutableObservable
is nowObservable
Observable.asImmutableObservable()
is nowObservable.asObservable()
Observable.value
is nowObservable.wrappedValue
Suggestions or feedback?
Feel free to create a pull request, open an issue or find me on Twitter.