Join Scott Gardner for an in-depth discussion in this video Explore units, part of Reactive Programming in iOS with RxSwift.
- [Voiceover] Units are part of the RxCocoa library. They are essentially structures that wrap observable sequences. To access a units observable sequence you call asObservable() on it. Units provide several conveniences. They exhibit the following characteristics. They are subscribed to and deliver events on the main thread via the main scheduler. And they are guaranteed not to emit error events. These characteristics make units particularly useful for working with UI elements. For example, displaying the most recent value in a UI label in that there will be no errors.
There's Driver who's intended use is to reactively bind an observable sequence to a UI element. Driver will also replay it's latest element if there is one to new subscribers. Using driver is optional. I like to use it because of it's characteristics and semantics. I'll show you in some examples next. ControlProperty is a reactive wrapper for a property of a UI element such as UI text fields, RxText, ControlProperty is a reactive wrapper around it's text property.
ControlProperty will also replay it's latest value to new subscribers. And ControlProperty will automatically emit a completed event when it's controller view is about to be de-allocated. Then there's ControlEvent which reactively wraps UI element events such a UI button's RxTap, ControlEvent wraps it's touch up inside event. ControlEvent will also emit a completed event automatically on deinit. They don't replay anything though. They only emit events as they occur to current subscribers.
While the standard operators that exist in the RxSwift library core follow the same naming convention as rx operators in other implementations such as rx.net and RxJS the naming convention for platform specific extensions such as units in RxCocoa is to use the rx_prefix to distinguish that they are reactive extensions not standard library methods or properties. Now that I've introduced Driver and Control units I can go over how to bind observable sequences to UI elements.
Learn reactive programming with Reactive Extensions for Swift and iOS by following along with author Scott Gardner. Scott introduces the Reactive Extensions libraries for Swift and Cocoa Touch, and shows how to work with observable sequences, bind user interface and data elements, perform networking operations, debug Rx code, and more.
- Using the Reactive Extensions library
- Working with observable sequences
- Binding UI and data elements
- Binding table and collection views
- Error handling