> For the complete documentation index, see [llms.txt](https://3440217568.gitbook.io/rxswift/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://3440217568.gitbook.io/rxswift/decision_tree/concat.md).

# concat

**让两个或多个 `Observables` 按顺序串连起来**

![](/files/-MWlwP7ZOpWgZo1qW5zw)

**concat** 操作符将多个 `Observables` 按顺序串联起来，当前一个 `Observable` 元素发送完毕后，后一个 `Observable` 才可以开始发出元素。

**concat** 将等待前一个 `Observable` 产生完成事件后，才对后一个 `Observable` 进行订阅。如果后一个是“热” `Observable` ，在它前一个 `Observable` 产生完成事件前，所产生的元素将不会被发送出来。

[startWith](/rxswift/decision_tree/startwith.md) 和它十分相似。但是 [startWith](/rxswift/decision_tree/startwith.md) 不是在后面添加元素，而是在前面插入元素。

[merge](/rxswift/decision_tree/merge.md) 和它也是十分相似。[merge](/rxswift/decision_tree/merge.md) 并不是将多个 `Observables` 按顺序串联起来，而是将他们合并到一起，不需要 `Observables` 按先后顺序发出元素。

## 演示

```swift
let disposeBag = DisposeBag()

let subject1 = BehaviorSubject(value: "🍎")
let subject2 = BehaviorSubject(value: "🐶")

let subject = BehaviorSubject(value: subject1)

subject
    .asObservable()
    .concat()
    .subscribe { print($0) }
    .disposed(by: disposeBag)

subject1.onNext("🍐")
subject1.onNext("🍊")

subject.onNext(subject2)

subject2.onNext("I would be ignored")
subject2.onNext("🐱")

subject1.onCompleted()

subject2.onNext("🐭")
```

**输出结果：**

```swift
next(🍎)
next(🍐)
next(🍊)
next(🐱)
next(🐭)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://3440217568.gitbook.io/rxswift/decision_tree/concat.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
