# 4. RxSwift 核心

这一章主要介绍 **RxSwift** 的核心内容：

![](https://4217506537-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWj545abP2yK86-qksJ%2Fsync%2F4b3276ae4b47b4d4337d24cfc90cfd1fcf8eb534.png?generation=1616819396601646\&alt=media)

* [Observable](https://3440217568.gitbook.io/rxswift/rxswift_core/observable) - 产生事件
* [Observer](https://3440217568.gitbook.io/rxswift/rxswift_core/observer) - 响应事件
* [Operator](https://3440217568.gitbook.io/rxswift/rxswift_core/operator) - 创建变化组合事件
* [Disposable](https://3440217568.gitbook.io/rxswift/rxswift_core/disposable) - 管理绑定（订阅）的生命周期
* [Schedulers](https://3440217568.gitbook.io/rxswift/rxswift_core/schedulers) - 线程队列调配

```swift
// Observable<String>
let text = usernameOutlet.rx.text.orEmpty.asObservable()

// Observable<Bool>
let passwordValid = text
    // Operator
    .map { $0.characters.count >= minimalUsernameLength }

// Observer<Bool>
let observer = passwordValidOutlet.rx.isHidden

// Disposable
let disposable = passwordValid
    // Scheduler 用于控制任务在那个线程队列运行
    .subscribeOn(MainScheduler.instance)
    .observeOn(MainScheduler.instance)
    .bind(to: observer)


...

// 取消绑定，你可以在退出页面时取消绑定
disposable.dispose()
```

下面几节会详细介绍这几个组件的功能和用法。

*ℹ️ 提示：这一章主要介绍一些偏理论方面的知识。你如果觉得阅读起来比较乏味的话，可以先**快速地浏览一遍**，了解 **RxSwift** 的核心组件大概有哪些内容。待以后遇到实际问题时，在回来查询。你可以直接跳到* [*更多例子*](https://3440217568.gitbook.io/rxswift/more_demo) *章节，去了解如何应用 **RxSwift**。*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://3440217568.gitbook.io/rxswift/rxswift_core.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
