# RxSwift — Reactive Programming for Swift > The Swift implementation of Reactive Extensions (Rx) providing observable sequences and operators for composing asynchronous and event-based programs on Apple platforms. ## Install Save as a script file and run: # RxSwift — Reactive Programming for Swift ## Quick Use ```swift import RxSwift import RxCocoa let disposeBag = DisposeBag() searchBar.rx.text.orEmpty .debounce(.milliseconds(300), scheduler: MainScheduler.instance) .distinctUntilChanged() .flatMapLatest { query in api.search(query) } .bind(to: tableView.rx.items(cellIdentifier: "Cell")) { _, item, cell in cell.textLabel?.text = item.name } .disposed(by: disposeBag) ``` ## Introduction RxSwift is the Swift implementation of the ReactiveX family of libraries. It provides a unified abstraction for asynchronous operations through observable sequences, enabling developers to compose complex event-driven logic with declarative operators instead of nested callbacks or delegates. ## What RxSwift Does - Wraps asynchronous events (network, UI, notifications) into observable streams - Provides 100+ operators for filtering, transforming, and combining sequences - Manages subscription lifecycle through DisposeBag automatic cleanup - Bridges UIKit controls to observables via the RxCocoa companion library - Enables reactive binding between ViewModels and Views in MVVM architecture ## Architecture Overview RxSwift is built around the Observable type that emits next, error, or completed events. Observers subscribe to these sequences, and Disposable objects manage subscription teardown. Schedulers abstract threading so operators can declare where work executes. RxCocoa extends UIKit classes with reactive properties (rx namespace) that produce or consume observable sequences. ## Self-Hosting & Configuration - Install via Swift Package Manager or CocoaPods with RxSwift and RxCocoa pods - Import RxRelay for publish/behavior subjects that never terminate with error - Configure custom schedulers for background processing with ConcurrentDispatchQueueScheduler - Use RxBlocking in unit tests to synchronously await observable results - Add RxTest for marble-diagram style testing with TestScheduler ## Key Features - DisposeBag pattern guarantees subscriptions are cleaned up on dealloc - Traits (Single, Completable, Maybe) encode intent in the type system - Driver and Signal traits guarantee main-thread delivery and no errors for UI binding - Comprehensive operator set matching the ReactiveX specification - Interoperability with Swift Concurrency via AsyncSequence bridges ## Comparison with Similar Tools - **Combine** — Apple's first-party framework; RxSwift supports older OS versions and has a larger operator set - **RxJava** — same ReactiveX family for Android; concepts transfer directly - **AsyncSequence** — built into Swift but lacks the rich operator library and UI bindings - **ReactiveCocoa** — earlier reactive Swift library; RxSwift follows the cross-language Rx specification ## FAQ **Q: Should I use RxSwift or Combine for new projects?** A: If you only target iOS 13+, Combine is viable. RxSwift offers broader OS support and a more mature operator ecosystem. **Q: How do I avoid retain cycles with RxSwift?** A: Use [weak self] in closures and rely on DisposeBag deallocation to break reference cycles. **Q: Can RxSwift work with SwiftUI?** A: Yes, you can bridge observables to Combine publishers using RxCombine, or wrap them in ObservableObject. **Q: What is the performance overhead?** A: Minimal for typical app use cases. Each subscription allocates a small object; hot paths with thousands of events per second may warrant profiling. ## Sources - https://github.com/ReactiveX/RxSwift - https://reactivex.io/ --- Source: https://tokrepo.com/en/workflows/asset-d1eaed88 Author: Script Depot