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)

Searching Database…

24.6K ·GitHub
这是干什么的?RxSwift 是 Reactive Extensions 标准在 Swift 平台上的实现——一个响应式编程库,它把异步操作、KVO 和 UI 事件都变成 Observable 序列,你可以自由地组合和转换它们。
它是一个大而全的库,还是分成好几个独立的部分?共有五个组件:RxSwift(Rx 核心)、RxCocoa(面向 iOS、macOS、watchOS 和 tvOS 应用的 Cocoa 和 UI 功能)、RxRelay(Publish、Behavior 和 Replay 三种 relay),以及用于测试的 RxTest 和 RxBlocking。
怎么把它加到我的应用里?有四种方式:用 Swift Package Manager(创建一个 Package.swift)、用 Carthage(添加到你的 Cartfile)、把发布包里带签名的 .xcframeworks 直接拖进你的 target,或者以 git 子模块的形式添加。
它会不会给我的项目拖进其他依赖?不会——RxSwift 没有任何外部依赖,添加它的时候不会带进来别的东西。
安装的时候有什么坑需要留意吗?Swift Package Manager 有一个已知的严重跨依赖 bug(SR-12303,2020 年初提交至今无人处理),RxSwift 也受它影响;README 提醒实际效果可能因情况而异,并链接了一个部分可行的解决办法。
如果我没接触过响应式编程,学起来难吗?有专门的入门资料:讲解为什么要用 Rx 以及基础知识的文档、测试技巧、常见错误,甚至还有 Rx 背后的数学原理,另外还有一本 RayWenderlich 的书和一些视频演讲。
它能在哪些 Apple 平台上用?RxCocoa 提供了开发常规 iOS、macOS、watchOS 和 tvOS 应用的能力,架在核心的 RxSwift 包之上。
实际的 RxSwift 代码长什么样?README 里的例子是一个 GitHub 仓库搜索:先对搜索框文字做 throttle,再用 flatMapLatest 转成网络请求,然后把得到的 observable 直接绑定到 table view 上,最后由 dispose bag 负责清理订阅。
在把它加进我自己的应用之前,我能先试试吗?可以——打开 Rx.xcworkspace,选择 RxExample scheme 然后点运行,就能编译全部代码并启动示例应用;还有 playground 可以让你交互式地试用各种操作符。
Single、Completable、Maybe 和 Driver 是什么?它们是 RxSwift 的 traits——针对常见场景专门定制的 observable 序列变体。README 的入门指南解释了每一种的用途以及它们为什么会存在。
跟 Apple 的 Combine 或者 ReactiveSwift 比怎么样?README 里有一条 'compare with Combine and ReactiveSwift' 的条目,链接到一份对比文档,方便你在做决定之前权衡各个选项。
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