RxSwift README

The project's README from github.com/ReactiveX/RxSwift. As one markdown file: README.md.

RxSwift Logo
Build Status Supported Platforms: iOS, macOS, tvOS, watchOS & Linux

Rx is a generic abstraction of computation expressed through Observable<Element> interface, which lets you broadcast and subscribe to values and other events from an Observable stream.

RxSwift is the Swift-specific implementation of the Reactive Extensions standard.

RxSwift Observable Example of a price constantly changing and updating the app's UI

While this version aims to stay true to the original spirit and naming conventions of Rx, this project also aims to provide a true Swift-first API for Rx APIs.

Cross platform documentation can be found on ReactiveX.io.

Like other Rx implementations, RxSwift's intention is to enable easy composition of asynchronous operations and streams of data in the form of Observable objects and a suite of methods to transform and compose these pieces of asynchronous work.

KVO observation, async operations, UI Events and other streams of data are all unified under abstraction of sequence. This is the reason why Rx is so simple, elegant and powerful.

I came here because I want to ...

... understand
... install
... hack around
... interact
... compare
... understand the structure

RxSwift is as compositional as the asynchronous work it drives. The core unit is RxSwift itself, while other dependencies can be added for UI Work, testing, and more.

It comprises five separate components depending on each other in the following way:

┌──────────────┐    ┌──────────────┐
│   RxCocoa    ├────▶   RxRelay    │
└───────┬──────┘    └──────┬───────┘
        │                  │
┌───────▼──────────────────▼───────┐
│             RxSwift              │
└───────▲──────────────────▲───────┘
        │                  │
┌───────┴──────┐    ┌──────┴───────┐
│    RxTest    │    │  RxBlocking  │
└──────────────┘    └──────────────┘

Usage

Here's an example In Action
Define search for GitHub repositories ...
let searchResults = searchBar.rx.text.orEmpty
    .throttle(.milliseconds(300), scheduler: MainScheduler.instance)
    .distinctUntilChanged()
    .flatMapLatest { query -> Observable<[Repository]> in
        if query.isEmpty {
            return .just([])
        }
        return searchGitHub(query)
            .catchAndReturn([])
    }
    .observe(on: MainScheduler.instance)
... then bind the results to your tableview
searchResults
    .bind(to: tableView.rx.items(cellIdentifier: "Cell")) {
        (index, repository: Repository, cell) in
        cell.textLabel?.text = repository.name
        cell.detailTextLabel?.text = repository.url
    }
    .disposed(by: disposeBag)

Installation

RxSwift doesn't contain any external dependencies.

These are currently the supported installation options:

Manual

Open Rx.xcworkspace, choose RxExample and hit run. This method will build everything and run the sample app

XCFrameworks

Each release starting with RxSwift 6 includes *.xcframework framework binaries.

Simply drag the needed framework binaries to your Frameworks, Libraries, and Embedded Content section under your target's General tab.

XCFrameworks instructions

[!TIP] RxSwift's xcframework(s) are signed with an Apple Developer account, and you can always verify the Team Name: Shai Mishali

XCFrameworks Signing Team Name Validation

Carthage

Add this to Cartfile

github "ReactiveX/RxSwift" "6.10.0"
$ carthage update

Carthage as a Static Library

Carthage defaults to building RxSwift as a Dynamic Library.

If you wish to build RxSwift as a Static Library using Carthage you may use the script below to manually modify the framework type before building with Carthage:

carthage update RxSwift --platform iOS --no-build
sed -i -e 's/MACH_O_TYPE = mh_dylib/MACH_O_TYPE = staticlib/g' Carthage/Checkouts/RxSwift/Rx.xcodeproj/project.pbxproj
carthage build RxSwift --platform iOS

Swift Package Manager

Note: There is a critical cross-dependency bug affecting many projects including RxSwift in Swift Package Manager. We've filed a bug (SR-12303) in early 2020 but have no answer yet. Your mileage may vary. A partial workaround can be found here.

Create a Package.swift file.

// swift-tools-version:5.0

import PackageDescription

let package = Package(
  name: "RxProject",
  dependencies: [
    .package(url: "https://github.com/ReactiveX/RxSwift.git", .upToNextMajor(from: "6.0.0"))
  ],
  targets: [
    .target(name: "RxProject", dependencies: ["RxSwift", .product(name: "RxCocoa", package: "RxSwift")]),
  ]
)
$ swift build

To build or test a module with RxTest dependency, set TEST=1.

$ TEST=1 swift test

Manually using git submodules

$ git submodule add [email protected]:ReactiveX/RxSwift.git

References

RxSwift (ReactiveX/RxSwift)

events
Searching Database…

24.6K ·GitHub
What is this?RxSwift is the Swift-specific implementation of the Reactive Extensions standard — a reactive programming library where async operations, KVO and UI events become Observable sequences you can compose and transform.
Is it one big library or several separate pieces?Five components: RxSwift (the Rx core), RxCocoa (Cocoa and UI capabilities for iOS, macOS, watchOS and tvOS apps), RxRelay (Publish, Behavior and Replay relays), and RxTest plus RxBlocking for testing.
How do I add it to my app?Four options: Swift Package Manager (create a Package.swift), Carthage (add to your Cartfile), dragging the signed .xcframeworks from a release into your target, or adding it as a git submodule.
Will it drag other dependencies into my project?No — RxSwift contains no external dependencies, so nothing else comes along when you add it.
Are there any install pitfalls to watch out for?Swift Package Manager has a known critical cross-dependency bug (SR-12303, filed early 2020 and still unanswered) that also affects RxSwift; the README warns results may vary and links a partial workaround.
Is it hard to pick up if I'm new to reactive programming?There is dedicated starter material: docs on why Rx and the basics, testing tips, common errors, and even the math behind Rx, plus a RayWenderlich book and video talks.
Which Apple platforms can I use it on?RxCocoa provides capabilities for general iOS, macOS, watchOS and tvOS app development, layered on top of the core RxSwift package.
What does real-world RxSwift code look like?The README's example is a GitHub repo search: throttle the search bar text, flatMapLatest into the network request, then bind the resulting observable directly to the table view, with a dispose bag cleaning up subscriptions.
Can I try it before adding it to my own app?Yes — open Rx.xcworkspace, pick the RxExample scheme and hit run to build everything and launch the sample app; playgrounds let you experiment with operators interactively.
What are Single, Completable, Maybe and Driver?They are RxSwift's traits — specialized flavors of observable sequences for common jobs. The README's getting-started guide explains what each one is for and why they exist.
How does it compare to Apple's Combine or ReactiveSwift?The README has a 'compare with Combine and ReactiveSwift' entry linking to a comparison, so you can weigh the options before committing.
What are similar projects?ReactiveCocoa, RxJS, bloc, fmdb, MBProgressHUD, pop, DZNEmptyDataSet, Moya
Creator
ReactiveX
Size
94.4 MB
Stars
24.6K
Age
11y
Commits · 90 Days