RxSwift Observable Loading State Management
Article Summary
Martin Moizard from BlaBlaCar tackles a common RxSwift pain point: how do you elegantly track loading states without breaking reactive programming principles?
When building iOS apps with RxSwift, displaying loaders during async operations (network calls, image processing, location services) often forces developers into imperative patterns or side effects. Moizard presents a cleaner approach that keeps your code purely reactive.
Key Takeaways
- Traditional .do() operator approach works but introduces unwanted side effects
- Custom .monitorLoading() extension splits streams into .data(), .loading(), and .error()
- Solution eliminates side effects while maintaining reactive programming philosophy
- Provides clean API for handling loading states and errors separately
A side effect free extension that transforms any Observable into separate data, loading, and error streams without compromising reactive principles.
About This Article
iOS developers using RxSwift often struggle to track loading states during async operations like network calls, image processing, and Core Location requests. The typical solutions push them toward imperative programming patterns that go against reactive principles.
Martin Moizard built a custom .monitorLoading() extension for ObservableType. It splits async operations into three streams: .data() returns results, .loading() tracks Boolean state, and .error() handles failures.
The extension keeps side effects out of the picture while preserving pure reactive streams. Developers can show or hide loaders without modifying external state or abandoning reactive programming.