RxSwift - Swift 中的响应式编程

Created at: 2015-04-08 05:25:17
Language: Swift
License: MIT

RxSwift 徽标
构建状态 支持的平台:iOS、macOS、tvOS、watchOS 和 Linux

Rx 是通过接口表示的计算的通用抽象,它允许你广播和订阅流中的值和其他事件。

Observable<Element>
Observable

RxSwift 是 Reactive Extensions 标准的 Swift 专用实现。

RxSwift  Observable 的 价格不断变化和更新应用 UI 的示例

虽然这个版本旨在忠于Rx的原始精神和命名约定,但这个项目也旨在为Rx API提供一个真正的Swift优先API。

跨平台文档可以在 ReactiveX.io 上找到。

与其他 Rx 实现一样,RxSwift 的目的是以对象的形式轻松组合异步操作和数据流,以及一套方法来转换和组合这些异步工作。

Observable

KVO 观察、异步操作、UI 事件和其他数据流都统一在序列的抽象下。这就是 Rx 如此简单、优雅和强大的原因。

我来这里是因为我想......

...理解
...安装
  • 将 RxSwift/RxCocoa 与我的应用程序集成。 安装指南
...四处乱窜
...互动
...比较
...了解结构

RxSwift 与它驱动的异步工作一样具有组合性。核心单元是 RxSwift 本身,而其他依赖项可以添加用于 UI 工作、测试等。

它由五个独立的组件组成,它们以以下方式相互依赖:

┌──────────────┐    ┌──────────────┐
│   RxCocoa    ├────▶   RxRelay    │
└───────┬──────┘    └──────┬───────┘
        │                  │        
┌───────▼──────────────────▼───────┐
│             RxSwift              │
└───────▲──────────────────▲───────┘
        │                  │        
┌───────┴──────┐    ┌──────┴───────┐
│    RxTest    │    │  RxBlocking  │
└──────────────┘    └──────────────┘
  • RxSwift: The core of RxSwift, providing the Rx standard as (mostly) defined by ReactiveX. It has no other dependencies.
  • RxCocoa: Provides Cocoa-specific capabilities for general iOS/macOS/watchOS & tvOS app development, such as Shared Sequences, Traits, and much more. It depends on both
    RxSwift
    and
    RxRelay
    .
  • RxRelay: Provides
    PublishRelay
    ,
    BehaviorRelay
    and
    ReplayRelay
    , three simple wrappers around Subjects. It depends on
    RxSwift
    .
  • RxTest and RxBlocking: Provides testing capabilities for Rx-based systems. It depends on
    RxSwift
    .

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)

Requirements

  • Xcode 12.x
  • Swift 5.x

For Xcode 11 and below, use RxSwift 5.x.

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

CocoaPods

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
    pod 'RxSwift', '6.6.0'
    pod 'RxCocoa', '6.6.0'
end

# RxTest and RxBlocking make the most sense in the context of unit/integration tests
target 'YOUR_TESTING_TARGET' do
    pod 'RxBlocking', '6.6.0'
    pod 'RxTest', '6.6.0'
end

Replace

YOUR_TARGET_NAME
and then, in the
Podfile
directory, type:

$ pod install

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.

Note: If you're using

RxCocoa
, be sure to also drag RxCocoaRuntime.xcframework before importing
RxCocoa
.

XCFrameworks 说明

Carthage

Add this to

Cartfile

github "ReactiveX/RxSwift" "6.6.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

  • Add RxSwift as a submodule
$ git submodule add git@github.com:ReactiveX/RxSwift.git
  • Drag
    Rx.xcodeproj
    into Project Navigator
  • Go to
    Project > Targets > Build Phases > Link Binary With Libraries
    , click
    +
    and select
    RxSwift
    ,
    RxCocoa
    and
    RxRelay
    targets

References